【发布时间】:2020-07-11 10:38:12
【问题描述】:
我在页面末尾抓取与 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())
【问题讨论】:
-
你认为为什么会有
li[9]?最好将所有li作为列表,然后使用[-1]从列表中获取最后一个元素 -
第一个地址
InvalidSelectorException然后移动到抓取。请edit the question 将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。
标签: python selenium web-scraping