【发布时间】:2022-01-23 21:06:50
【问题描述】:
我正在使用 BeautifulSoup 从以下 URL 抓取网页:https://bitinfocharts.com/top-100-richest-dogecoin-addresses-2.html
我能够抓取左侧超链接内的网页,但现在我正在尝试为我抓取的页面创建一些参数。我正在使用的参数是右侧的“最后出”日期。基本上,我试图只抓取具有特定日期的 Last Out 的网页。例如,仅抓取 2020 年 1 月 1 日之后的页面。
我认为需要做的是有一个 if 语句,如果日期高于 1-1-2020,那么它将继续抓取相应的超链接。不过我不太确定,或者是否可以用 Beautiful Soup 做到这一点。
感谢任何帮助、想法或建议。
import csv
import requests
from bs4 import BeautifulSoup as bs
headers = []
datarows = []
with requests.Session() as s:
s.headers = {"User-Agent": "Safari/537.36"}
r = s.get('https://bitinfocharts.com/top-100-richest-dogecoin-addresses-2.html')
soup = bs(r.content, 'lxml')
address_links = [i['href'] for i in soup.select('.table td:nth-child(2) > a')]
for url in address_links:
r = s.get(url)
soup = bs(r.content, 'lxml')
table = soup.find(id="table_maina")
if table:
item = soup.find('h1').text
newitem = item.replace('Dogecoin','')
finalitem = newitem.replace('Address','')
for row in table.find_all('tr'):
heads = row.find_all('th')
if heads:
headers = [th.text for th in heads]
else:
datarows.append([td.text for td in row.find_all('td')])
fcsv = csv.writer(open(f'{finalitem}.csv', 'w', newline=''))
fcsv.writerow(headers)
fcsv.writerows(datarows)
【问题讨论】:
标签: python web-scraping beautifulsoup