我在页面的末尾抓取与Infosys相关的新闻文章,但出现了错误 selenium.common.exceptions.InvalidSelectorException:消息:选择器无效。 希望删除所有与Infosys相关的文章
from bs4 import BeautifulSoup
import re
from selenium import webdriver
import chromedriver_binary
import string
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome("/Users/abhishekgupta/Downloads/chromedriver")
driver.get("https://finance.yahoo.com/quote/INFY/news?p=INFY")
for i in range(20): # adjust integer value for need
# you can change right side number for scroll convenience or destination
driver.execute_script("window.scrollBy(0, 250)")
# you can change time integer to float or remove
time.sleep(1)
print(driver.find_element_by_xpath('//*[@id="latestQuoteNewsStream-0-Stream"]/ul/li[9]/div/div/div[2]/h3/a/text()').text())
我认为您的代码很好,只有一件事:当我们在selenium中使用xpath检索文本或链接时,与scrapy相比,或者如果您使用的是lxml fromstring library,那么这里有一些应该适合您的东西
即使这样做,它也会以同样的方式工作,因为只有一个元素具有此id,所以只需使用
可以使用
//
而不是/div/div/div[2]
使用不太详细的xpath若您想要最后一项,那个么获取所有
li
作为列表,然后使用[-1]
获取列表上的最后一个元素页面中不存在您提供的xPath
下载xPath FinderChrome扩展以查找文章的正确xPath
下面是文章列表的xPath示例,您需要通过id进行循环:
相关问题 更多 >
编程相关推荐