Python 3.x 为什么我的Selenium代码只返回我请求的数据的一半

Python 3.x 为什么我的Selenium代码只返回我请求的数据的一半,python-3.x,selenium-webdriver,web-scraping,selenium-chromedriver,Python 3.x,Selenium Webdriver,Web Scraping,Selenium Chromedriver,最近,我编写了一个selenium web scraper,旨在提取一个表中的所有信息,该表包含美国所有总统选举的数据。该表位于维基百科网站上 问题是,当我将结果写入.txt文件时,代码会返回我需要的所有信息。但每当我尝试在文本编辑器中打印相同的结果时,它只返回我所需数据的一半。我不明白问题是什么。有人能帮我吗 这是我的密码 from selenium import webdriver from bs4 import BeautifulSoup from selenium.webdriver.c

最近,我编写了一个selenium web scraper,旨在提取一个表中的所有信息,该表包含美国所有总统选举的数据。该表位于维基百科网站上

问题是,当我将结果写入.txt文件时,代码会返回我需要的所有信息。但每当我尝试在文本编辑器中打印相同的结果时,它只返回我所需数据的一半。我不明白问题是什么。有人能帮我吗

这是我的密码

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import pandas

# using selenium and shromedriver to extract the javascript wikipage

scrape_options = Options()
scrape_options.add_argument('--headless')
driver = webdriver.Chrome(r'web scraping master/chromedriver', options=scrape_options)
page_info = driver.get('https://en.wikipedia.org/wiki/United_States_presidential_election')

# waiting for the javascript to load


try:
    WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME,"wikitable")))
finally:
    page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable jquery-tablesorter'})
print(table)
with open("loge.txt","w") as f: #Only part I added to the code
    f.write(str(table))

我不确定问题出在哪里,但这一切都如期而至。我已将
loge.txt
更改为
loge.html
,代码将转储整个表

介意试试这个吗

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

scrape_options = Options()
scrape_options.add_argument('--headless')
driver = webdriver.Chrome(options=scrape_options)
page_info = driver.get('https://en.wikipedia.org/wiki/United_States_presidential_election')

try:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "wikitable")))
finally:
    page = driver.page_source

soup = BeautifulSoup(page, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable jquery-tablesorter'})

with open("loge.html", "w") as f:
    f.write(str(table))

该代码在命令提示下也能正常工作。我的文本编辑器有什么问题吗?谢谢你的回答,问题是代码将完整的表完全转储到文件中,但当我尝试在文本编辑器中显示信息时,它只转储了表的一半。