发送不同的 json 请求但在 Python 中得到相似的结果
Sending different json requests but getting similar results in Python
我正在尝试发送 json 请求以抓取 this link 中的无限滚动元素。不过,我知道有些参数不是必需的,但可以肯定的是,我定义了完全相同的参数发送到 server:my 的参数和代码是
import requests
import json
parameters1 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12%3A38+p.m.+Oct.+24%2C+2016', 'docId':'',
'docType':'806','sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8541','count':'10',
'channelName':'%2Fnews%2Flatest%2Fcompany%2Fus%2Fxom', 'topic':'',
'_':'1479888927416' }
parameters2 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12%3A38+p.m.+Oct.+24%2C+2016','docId':'',
'docType':'806' ,'sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8525','count':'10',
'channelName':'%2Fnews%2Flatest%2Fcompany%2Fus%2Fxom', 'topic':'',
'_':'1479888927417' }
firstUrl = "http://www.marketwatch.com/news/headline/getheadlines"
html1 = requests.get(firstUrl, params = parameters1)
result1 = (json.loads(html1.text))
html2 = requests.get(firstUrl, params = parameters2)
result2 = (json.loads(html2.text))
然后我检查它们是否相同:
if(result2 == result1):
print(True)
答案总是正确的。我更改了很多参数,但没有用。我的代码或程序有什么问题?
您的问题是,您正在发送 JSON 但使用编码字符串。而不是 %2F
你应该使用 /
,而不是 +
一个空格,而不是 %3A
一个 :
等等。您可以解码您的字符串,例如 on this site.
import requests
import json
parameters1 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12:38 p.m. Oct., 2016', 'docId':'',
'docType':'806','sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8541','count':'10',
'channelName':'/news/latest/company/us/xom', 'topic':'',
'_':'1479888927416' }
parameters2 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12:38 p.m. Oct., 2016','docId':'',
'docType':'806' ,'sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8525','count':'10',
'channelName':'/news/latest/company/us/xom', 'topic':'',
'_':'1479888927417' };
firstUrl = "http://www.marketwatch.com/news/headline/getheadlines"
html1 = requests.get(firstUrl, params = parameters1)
result1 = (json.loads(html1.text))
html2 = requests.get(firstUrl, params = parameters2);
result2 = (json.loads(html2.text))
那么result1==result2
就是False
我正在尝试发送 json 请求以抓取 this link 中的无限滚动元素。不过,我知道有些参数不是必需的,但可以肯定的是,我定义了完全相同的参数发送到 server:my 的参数和代码是
import requests
import json
parameters1 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12%3A38+p.m.+Oct.+24%2C+2016', 'docId':'',
'docType':'806','sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8541','count':'10',
'channelName':'%2Fnews%2Flatest%2Fcompany%2Fus%2Fxom', 'topic':'',
'_':'1479888927416' }
parameters2 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12%3A38+p.m.+Oct.+24%2C+2016','docId':'',
'docType':'806' ,'sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8525','count':'10',
'channelName':'%2Fnews%2Flatest%2Fcompany%2Fus%2Fxom', 'topic':'',
'_':'1479888927417' }
firstUrl = "http://www.marketwatch.com/news/headline/getheadlines"
html1 = requests.get(firstUrl, params = parameters1)
result1 = (json.loads(html1.text))
html2 = requests.get(firstUrl, params = parameters2)
result2 = (json.loads(html2.text))
然后我检查它们是否相同:
if(result2 == result1):
print(True)
答案总是正确的。我更改了很多参数,但没有用。我的代码或程序有什么问题?
您的问题是,您正在发送 JSON 但使用编码字符串。而不是 %2F
你应该使用 /
,而不是 +
一个空格,而不是 %3A
一个 :
等等。您可以解码您的字符串,例如 on this site.
import requests
import json
parameters1 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12:38 p.m. Oct., 2016', 'docId':'',
'docType':'806','sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8541','count':'10',
'channelName':'/news/latest/company/us/xom', 'topic':'',
'_':'1479888927416' }
parameters2 = {'ticker':'XOM', 'countryCode':'US',
'dateTime':'12:38 p.m. Oct., 2016','docId':'',
'docType':'806' ,'sequence':'e5a00f51-8821-4fbc-8ac6-e5f64b5eb0f2',
'messageNumber':'8525','count':'10',
'channelName':'/news/latest/company/us/xom', 'topic':'',
'_':'1479888927417' };
firstUrl = "http://www.marketwatch.com/news/headline/getheadlines"
html1 = requests.get(firstUrl, params = parameters1)
result1 = (json.loads(html1.text))
html2 = requests.get(firstUrl, params = parameters2);
result2 = (json.loads(html2.text))
那么result1==result2
就是False