【问题标题】:Scraping news articles only beyond a certain date on Reuters [closed]仅在路透社的某个日期之后抓取新闻文章[关闭]
【发布时间】:2021-11-13 12:37:13
【问题描述】:

我想从Link 中提取新闻文章随着您不断向下滚动,旧文章不断出现。但我只想要过去 1 年的信息。如何设置过滤器?

【问题讨论】:

  • 到目前为止你尝试过什么?在问题中发布相同的内容。
  • @pmadhu 我想不出任何办法来解决这个问题

标签: selenium web-scraping beautifulsoup


【解决方案1】:

像这样试试。

下面的代码滚动直到找到18 days ago。将条件更改为a year ago,当找到一年前的新闻时,循环将中断。

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://www.reuters.com/companies/AAPL.O")

i=0
try:
    while True:
        news = driver.find_elements_by_xpath("//div[@class='item']")
        driver.execute_script("arguments[0].scrollIntoView(true);", news[i])
        if news[i].find_element_by_tag_name("time").get_attribute("innerText") == "18 days ago":
            break
        print(news[i].find_element_by_tag_name("a").get_attribute("innerText"))
        i += 1
        time.sleep(.5)
except:
    pass

driver.quit()

【讨论】:

  • 收到此错误:HTTPConnectionPool(host='127.0.0.1', port=40007): url: /session/f6de1e58f557727793481d09cf44e2b5/window/maximize 超过最大重试次数(由 NewConnectionError(':无法建立新连接:[Errno 111] Connection refused'))
  • @huy - 请参阅此Link。我不认为这是因为我的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 1970-01-01
  • 2012-01-01
  • 2022-08-10
相关资源
最近更新 更多