我有一段代码正在解析嵌入在此页面源代码中的JSON外观对象:
用于执行此操作的代码是:
import json
import requests
import re
url = 'http://www.whoscored.com/Matches/829726/Live/England-Premier-League-2014-2015-Stoke-Manchester-United'
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',
'X-Requested-With': 'XMLHttpRequest',
'Host': 'www.whoscored.com',
'Referer': 'http://www.whoscored.com/'}
r = requests.get(url, headers=headers)
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.content)
data_cen = re.compile('var matchCentreData = ({.*?})')
event_type = re.compile('var matchCentreEventTypeJson = ({.*?})')
data = soup.find("a", href="/ContactUs").find_next("script").text
d = json.dumps(data_cen.search(data).group(1))
e = json.dumps(event_type.search(data).group(1))
data_dict = json.loads(d)
event_dict = json.loads(e)
print(event_dict)
print(data_dict)
当我在Windows平台上运行它时,无论是在Python 2.7 IDLE,Command Shell还是iPython中,都不会返回正在解析的整个对象(它非常长)。返回的物体似乎在大约一半的时间内切断了。
这会导致行noneType
出现e = json.dumps(event_type.search(data).group(1))
错误,因为它位于数据结构底部附近,而不会打印到我的操作系统上的屏幕上。另一个用户在基于Linux的操作系统上运行此代码,但没有遇到同样的问题。
是否有人知道返回字符串最大长度周围的任何Windows特定问题?或者其他任何可能导致这种情况发生在Windows中而不是Linux中的问题?
由于