【发布时间】:2021-08-10 07:10:33
【问题描述】:
我想抓取为期 6 天的日期和相应的新闻标题/文章——比如今天运行 python 脚本时,它应该从今天(8 月 10 日)到 8 月 4 日抓取标题/文章。 截至目前,我可以从here 抓取所有日期的日期和标题/网址。 这是相同的代码
websites = ['https://www.thespiritsbusiness.com/tag/rum/']
for spirits in websites:
browser.get(spirits)
time.sleep(1)
news_links = browser.find_elements_by_xpath('//*[@id="archivewrapper"]/div/div[2]/h3')
n_links = [ele.find_element_by_tag_name('a').get_attribute('href') for ele in news_links]
dates = browser.find_elements_by_xpath('//*[@id="archivewrapper"]/div/div[2]/small')
n_dates = [ele.text for ele in dates]
print(n_links)
print(n_dates)
但是,我如何从今天开始的最后 6 天刮一段?有什么想法吗?
【问题讨论】:
-
使用
datetime模块。 -
@Ram :我也想过,但是网站文章中提到的日期都是文本格式的。所以如果阈值到 6 然后 datetime.today()- 从网站是我的想法......但是..idk。
-
然后从该字符串中提取日期。
标签: python selenium selenium-webdriver web-scraping