【发布时间】:2014-10-04 20:34:00
【问题描述】:
我在 Windows Vista 64 位上使用 Python.org 版本 2.7 64 位。我有一些代码是对我在这里提出的问题的答案的开发:
Cannot locate displayed data in source code when Scraping with Scrapy
我对这个答案的开发是遍历一个范围内的一系列日期并获取我在每个日期之后的数据(想法是如果该日期没有数据,我将返回空白) .
代码是这样的:
from datetime import date, timedelta as td
from ast import literal_eval
from datetime import datetime
import requests
d1 = date(2013,11,01)
d2 = date(2014,5,31)
delta = d2 - d1
for i in range(delta.days + 1):
time1 = str(d1 + td(days=i))
time2 = time1.split("-", 1)[0]
time3 = time1.split("-", -1)[1]
time4 = time1.rsplit("-", 1)[-1]
time2 = int(time2)
time3 = int(time3)
time4 = int(time4)
date = datetime(year=time2, month=time3, day=time4)
print date
url = 'http://www.whoscored.com/tournamentsfeed/8273/Fixtures/'
params = {'d': date.strftime('%Y%m'), 'isAggregate': 'false'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'}
response = requests.get(url, params=params, headers=headers)
fixtures = literal_eval(response.content)
print fixtures
这段代码不是只为我提供任何一天的固定装置,而是为我提供该日期所在月份的整个固定装置。
然后代码在迭代过程中继续落在随机点上并抛出以下错误:
Traceback (most recent call last):
File "C:\Python27\newtets\newtets\spiders\test3.py", line 32, in <module>
fixtures = literal_eval(response.content)
File "C:\Python27\lib\ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "C:\Python27\lib\ast.py", line 37, in parse
return compile(expr, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
^
SyntaxError: invalid syntax
我想知道的是:
1) 如果没有固定装置,为什么此代码结构不返回当天的任何一个/或固定装置/null?
2)这个错误的原因是什么,为什么在执行过程中总是随机出现?
谢谢
【问题讨论】:
标签: python xmlhttprequest scrapy