为了账号安全,请及时绑定邮箱和手机立即绑定

获取抓取日期 - Python & BeautifulSoup4

获取抓取日期 - Python & BeautifulSoup4

慕虎7371278 2024-01-16 15:44:22
我想使用以下代码在 CSV 文件中报告抓取的确切日期:import bs4from bs4 import BeautifulSoupimport requestsimport pandasfrom pandas import DataFrameimport csvwith open('sortlist_scrap.csv', mode='w') as csv_file:   fieldnames = ['Link', 'Rank', 'Date']   writer = csv.DictWriter(csv_file, fieldnames=fieldnames)   writer.writeheader()Found_link = []Found_Rank = []Found_date = []url = "https://www.sortlist.fr/identite-visuelle/paris-fr?page={}"for page_num in range(1, 100):   page = requests.get(url.format(page_num))link = soup.select_one('a[title*="Dupont Lewis"]')previous_md_headlines = link.find_all_previous("a", {"class": "md-headline"})next_md_headlines = link.find_all_next("a", {"class": "md-headline"})position = 50 - len(next_md_headlines )today = date.today()page = sortlistscrapFound_link.append(page)Found_Rank.append(position)Found_date.append(today)data = { 'Found_Rank': Rank,'Found_link':Found_link, 'Found_date':article_date}df = DataFrame(data, columns = ['Found_link','Found_Rank','Found_date'])df.to_csv(r'C:\Users\MacOS\Desktop\sortlist_scrap.csv')但我收到以下错误:NameError:名称“日期”未定义有人可以帮忙吗?谢谢!
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

您可能想使用datetime.datetime.today(). 所以首先添加from datetime import datetime到代码的开头。并使用today = datetime.today()代替today = date.today()



查看完整回答
反对 回复 2024-01-16
  • 1 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信