2017-04-21 158 views
0

我試圖自動執行將文本從網站傳遞到工具的過程,以便估計文本的閱讀級別。但是,當我通過post方法傳遞url編碼的文本時,我得到錯誤400錯誤的請求。錯誤的請求錯誤400請求庫文件

article = 'The quick brown fox jumps over the lazy dog.' 
headers = ({'Host': 'auto-ilr.ll.mit.edu', 
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0', 
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
    'Accept-Language': 'en-US,en;q=0.5', 
    'Accept-Encoding': 'gzip, deflate', 
    'DNT': '1', 
    'Referer': 'https://auto-ilr.ll.mit.edu/instant/', 
    'Connection': 'keep-alive'}) 
s = requests.Session() 
#s.mount('https://', SSLAdapter()) 
s.mount('https://', MyAdapter()) 
try: 
    postdata = urllib.parse.urlencode({'Language': 'English', 'Text': article}) 
    soup = s.post('https://auto-ilr.ll.mit.edu/instant/summary3', data=postdata, headers = headers, verify=False) 

我不知道有什麼區別,但也出現了少數情況下請求所經歷和最終湯變量,從網站的文字結束了,但它是文本顯示的網站做沒有正確處理我包括的文本。

我感覺我缺少一些簡單的東西。

回答

1

簡單的東西,你不必編碼datarequests會爲你:

article = 'The quick brown fox jumps over the lazy dog.' 
headers = { 
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0', 
    'Referer': 'https://auto-ilr.ll.mit.edu/instant/' 
} 
postdata = {'Language': 'English', 'Text': article} 
s = requests.Session() 
soup = s.post('https://auto-ilr.ll.mit.edu/instant/summary3', data=postdata, headers = headers, verify=False) 

print(soup.status_code) 

而且,你不必把所有的標題,馬布只是「用戶 - 代理「或」引用者「。