【发布时间】:2017-03-31 10:37:16
【问题描述】:
我已经构建了一个非常简单的网络爬虫来爬取下面 URL 中的大约 100 个小 json 文件。问题是爬虫需要一个多小时才能完成。考虑到 json 文件的大小,我发现这很难理解。我在这里做错了什么吗?
def get_senate_vote(vote):
URL = 'https://www.govtrack.us/data/congress/113/votes/2013/s%d/data.json' % vote
response = requests.get(URL)
json_data = json.loads(response.text)
return json_data
def get_all_votes():
all_senate_votes = []
URL = "http://www.govtrack.us/data/congress/113/votes/2013"
response = requests.get(URL)
root = html.fromstring(response.content)
for a in root.xpath('/html/body/pre/a'):
link = a.xpath('text()')[0].strip()
if link[0] == 's':
vote = int(link[1:-1])
try:
vote_json = get_senate_vote(vote)
except:
return all_senate_votes
all_senate_votes.append(vote_json)
return all_senate_votes
vote_data = get_all_votes()
【问题讨论】:
标签: python json web-crawler