我想讀通過URL Twitter的飼料。昨天我能拉使用的代碼和一些80K鳴叫,由於我的機器上的一些更新,我的Mac終端停止完成Python代碼之前響應。urllib.request.urlopen是表現奇怪。第二天不返回數據。爲什麼?
今天,同樣的代碼不會返回任何JSON數據。這是扔給我空的結果。雖然如果我在瀏覽器中輸入相同的URL,我可以得到一個充滿數據的json文件。
這是我的代碼: 方法1:
try:
urllib.request.urlcleanup()
response = urllib.request.urlopen(url)
print('URL to used: ', url)
testURL = response.geturl()
print('URL you used: ', testURL)
jsonResponse = response.read()
jsonResponse = urllib.request.urlopen(url).read()
該印刷:
URL to used: https://twitter.com/i/search/timeline?f=tweets&q=%20since%3A2017-08-14%20until%3A2017-08-15%20USA&src=typd&max_position=
URL you used: https://twitter.com/i/search/timeline?f=tweets&q=%20since%3A2017-08-14%20until%3A2017-08-15%20USA&src=typd&max_position=
json: {'items_html': '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n', 'focused_refresh_interval': 30000, 'has_more_items': False, 'min_position': 'TWEET--', 'new_latent_count': 0}
****方法2:****
try:
request = urllib.request.Request(url, headers=headers)
except:
print("Thats the problem here:")
try:
response = urllib.request.urlopen(request)
except:
print("Exception while fetching response")
testURL = response.geturl()
print('URL you used: ', testURL)
try:
jsonResponse = response.read()
except:
print("Exception while reading response")
在這兩種情況下結果相同。
請幫忙。
我得到同樣的響應,你怎麼做。 – elena
奇怪的是正確的。有時我得到它,有時不 –
嘛,我不要在瀏覽器中得到合理的迴應要麼。 – elena