【发布时间】:2017-07-20 15:34:43
【问题描述】:
有人知道下面的代码有什么问题吗?它仅从一天中获取数据。然而,该网页是一个动态网络数据库,包含多年的数据。我需要抓取 2013-2016 年每个月和每天的数据并存储到 CSV 文件中。
import calendar
import requests
from bs4 import BeautifulSoup
cal = calendar.Calendar()
base_url = 'http://www.pse.pl/index.php?modul=21&id_rap=24&data=2016'
month_url = '&Month='
day_url = '&Day='
for year in range(2015, 2017):
for month in range(1, 13):
monthdays = [d for d in cal.itermonthdays(year, month) if d != 0]
for day in monthdays:
r = requests.get(base_url + str(year) + month_url + str(month) + day_url + str(day))
soup = BeautifulSoup(r.text,'lxml')
findtable = soup.find('table',{'id':'tabela'})
for i in findtable.findAll('tr'):
for j in i.findAll('td'):
print (j.text)
【问题讨论】:
标签: python calendar beautifulsoup screen-scraping