Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium Python无法提取所有span标记中的文本_Python_Selenium_Selenium Chromedriver_Screen Scraping - Fatal编程技术网

Selenium Python无法提取所有span标记中的文本

Selenium Python无法提取所有span标记中的文本,python,selenium,selenium-chromedriver,screen-scraping,Python,Selenium,Selenium Chromedriver,Screen Scraping,我正在创建一个小型python程序,它可以自动运行10fastfingers。为了做到这一点,我必须首先提取我必须键入的所有单词。所有这些单词都存储在span标记中,如下所示: 当我运行代码时,它只提取前20-30个单词,而不是提取所有单词。为什么会这样?这是我的密码: from selenium import webdriver import time url = "https://10fastfingers.com/typing-test/english" brow

我正在创建一个小型python程序,它可以自动运行10fastfingers。为了做到这一点,我必须首先提取我必须键入的所有单词。所有这些单词都存储在
span
标记中,如下所示:

当我运行代码时,它只提取前20-30个单词,而不是提取所有单词。为什么会这样?这是我的密码:

from selenium import webdriver
import time

url = "https://10fastfingers.com/typing-test/english"

browser = webdriver.Chrome("D:\\Python_Files\\Programs\\chromedriver.exe")

browser.get(url)

time.sleep(10)

count = 1

wordlst = []

while True:
    
    try:
        word = browser.find_element_by_xpath(f'//*[@id="row1"]/span[{count}]')
        wordlst.append(word.text)
        count += 1
        
    except:
        break

print(wordlst)
输出:

“他们”、“如何”、“说”、“光”、“显示”、“似乎”、“不是”、“两个”、“在下面”、“听到”、“他们”、“那里”、“关于”、“脸”、“我们”、“改变”、“年”、“只有”、“离开”、“数字”、“找到”、“父亲”、“人”、“房子”、“真的”、“我的”、“拼写”、“何时”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“看”、“', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 


如何解决此问题?如果您有任何帮助,我们将不胜感激。谢谢!

您可以使用BeautifulSoup来解决此问题

from selenium import webdriver
import time
from bs4 import BeautifulSoup

url = "https://10fastfingers.com/typing-test/english"

browser = webdriver.Chrome("D:\\Python_Files\\Programs\\chromedriver.exe")
browser.get(url)
time.sleep(3)
html_soup = BeautifulSoup(browser.page_source, 'html.parser')
div = html_soup.find_all('div', id = 'row1')
wordlst=div[0].get_text().split()
browser.quit()
print(wordlst)

为了继续你的方法

from selenium import webdriver
import time

url = "https://10fastfingers.com/typing-test/english"
browser = webdriver.Chrome("D:\\Python_Files\\Programs\\chromedriver.exe")
browser.get(url)
time.sleep(6)
wordlst=browser.find_elements_by_xpath('//div[@id="row1"]/span')
wordlst=[x.get_attribute("innerHTML") for x in wordlst]
browser.quit()
print(wordlst)

嘿!太棒了!你能解释一下什么是
wordlst=div[0].get_text().split()吗
确实是这样吗?我没有在
BeautifulSoup
上做过很多工作,所以我无法理解它到底是做什么的。当然。Div返回一个id为“row1”的所有元素的列表。
get_text
给出
Div
标记之间的所有文本,包括span中删除标记时的文本。现在既然您想要一个l单词列表中,我添加了split()。在pythonOk中,beautifulsou通常用于轻松处理html…Thx作为解释!但是我的方法有什么错?你的方法没有什么错。它实际上有一个非常简单的解决方案。将
wordlst.append(word.text)
替换为
wordlst.append(word.get\u属性(“innerHTML”))
。实际上,只需使用
find\u elements\u by\u xpath
就可以使代码变得非常小。好吧……我按照相同的逻辑提取了所有要在多人游戏中键入的单词,但我得到了一个不同的输出。你愿意帮我吗?所有多人游戏的url都是在登录后创建的,这是我尝试的:
html\u soup=soup(browser.page_source,'html.parser')div=html_soup.find_all('div',id='game')wordlst=div[0]。get_text().split()print(wordlst)