簡體   English   中英

發送不同的json請求但在Python中獲得相似的結果

[英]Sending different json requests but getting similar results in Python

我正在嘗試發送json請求以在此鏈接中刮取無限滾動元素。盡管我知道一些參數不是必需的,但可以肯定的是,我定義了與發送到服務器的參數完全相同的參數:我的參數和代碼是

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 a :等。 例如您可以在此站點上解碼字符串。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM