Python 刮掉雅虎股票新闻

Python 刮掉雅虎股票新闻,python,selenium,web-scraping,Python,Selenium,Web Scraping,我在页面的末尾抓取与Infosys相关的新闻文章,但出现了错误 selenium.common.exceptions.InvalidSelectorException:消息:选择器无效。 希望删除所有与Infosys相关的文章 from bs4 import BeautifulSoup import re from selenium import webdriver import chromedriver_binary import string import time from seleniu

我在页面的末尾抓取与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())


页面中不存在您提供的xPath

下载Chrome扩展以查找文章的正确xPath

下面是文章列表的xPath示例,您需要通过id进行循环:

/html/body/div[1]/div/div/div[1]/div/div[3]/div[1]/div/div[5]/div/div/div/ul/li[ID]/div/div/div[2]/h3/a/u

页面中不存在您提供的xPath

下载Chrome扩展以查找文章的正确xPath

下面是文章列表的xPath示例,您需要通过id进行循环:

/html/body/div[1]/div/div/div[1]/div/div[3]/div[1]/div/div[5]/div/div/div/ul/li[ID]/div/div/div[2]/h3/a/u

您可以使用不太详细的xpath,使用
/
而不是
/div/div/div[2]

若你们想要最后一个项目,那个么把所有的
li
作为列表,然后使用
[-1]
来获取列表中的最后一个元素

from selenium import webdriver
import time

driver = webdriver.Chrome("/Users/abhishekgupta/Downloads/chromedriver")
#driver = webdriver.Firefox()

driver.get("https://finance.yahoo.com/quote/INFY/news?p=INFY")

for i in range(20):
       driver.execute_script("window.scrollBy(0, 250)")
       time.sleep(1)

all_items = driver.find_elements_by_xpath('//*[@id="latestQuoteNewsStream-0-Stream"]/ul/li')

#for item in all_items:
#    print(item.find_element_by_xpath('.//h3/a').text)
#    print(item.find_element_by_xpath('.//p').text)
#    print('---')
    
print(all_items[-1].find_element_by_xpath('.//h3/a').text)
print(all_items[-1].find_element_by_xpath('.//p').text)

您可以使用不太详细的xpath,使用
/
而不是
/div/div/div[2]

若你们想要最后一个项目,那个么把所有的
li
作为列表,然后使用
[-1]
来获取列表中的最后一个元素

from selenium import webdriver
import time

driver = webdriver.Chrome("/Users/abhishekgupta/Downloads/chromedriver")
#driver = webdriver.Firefox()

driver.get("https://finance.yahoo.com/quote/INFY/news?p=INFY")

for i in range(20):
       driver.execute_script("window.scrollBy(0, 250)")
       time.sleep(1)

all_items = driver.find_elements_by_xpath('//*[@id="latestQuoteNewsStream-0-Stream"]/ul/li')

#for item in all_items:
#    print(item.find_element_by_xpath('.//h3/a').text)
#    print(item.find_element_by_xpath('.//p').text)
#    print('---')
    
print(all_items[-1].find_element_by_xpath('.//h3/a').text)
print(all_items[-1].find_element_by_xpath('.//p').text)

我认为您的代码很好,只有一件事:当我们在selenium中使用xpath检索文本或链接时,与scrapy相比,或者如果您使用的是lxml fromstring library,那么这里有一些应该适合您的东西

#use this code for printing instead 
print(driver.find_element_by_xpath('//*[@id="latestQuoteNewsStream-0- Stream"]/ul/li[9]/div/div/div[2]/h3/a').text)
即使这样做,它也会以同样的方式工作,因为只有一个元素具有此id,所以只需使用

#This should also work fine
print(driver.find_element_by_xpath('//*[@id="latestQuoteNewsStream-0- Stream"]').text)

我认为您的代码很好,只有一件事:当我们在selenium中使用xpath检索文本或链接时,与scrapy相比,或者如果您使用的是lxml fromstring library,那么这里有一些应该适合您的东西

#use this code for printing instead 
print(driver.find_element_by_xpath('//*[@id="latestQuoteNewsStream-0- Stream"]/ul/li[9]/div/div/div[2]/h3/a').text)
即使这样做,它也会以同样的方式工作,因为只有一个元素具有此id,所以只需使用

#This should also work fine
print(driver.find_element_by_xpath('//*[@id="latestQuoteNewsStream-0- Stream"]').text)

你认为为什么会有
li[9]
?最好将所有
li
作为列表,然后使用
[-1]
从列表中获取最后一个元素第一个地址
InvalidSelectorException
,然后移动到抓取。请将其限制在一个特定的问题上,并提供足够的详细信息,以确定适当的答案。避免同时问多个不同的问题。请参阅帮助页面以澄清此问题。您认为为什么会出现
li[9]
?最好将所有
li
作为列表,然后使用
[-1]
从列表中获取最后一个元素第一个地址
InvalidSelectorException
,然后移动到抓取。请将其限制在一个特定的问题上,并提供足够的详细信息,以确定适当的答案。避免同时问多个不同的问题。请参阅页面以获取澄清此问题的帮助。