我想使用以下代码在 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()
添加回答
举报
0/150
提交
取消