urllib2.HTTPError: HTTP Error 400: Bad Request

來源:互聯網
上載者:User

標籤:bad   log   err   進位   mac os x   api   import   解決問題   nec   

 1 import urllib2 2 import re 3 import os 4   5 def process_item(self, item, spider): 6         headers = { 7                     "Host":    ‘img31.mtime.cn‘, 8                     "User-Agent":    ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:53.0) Gecko/20100101 Firefox/53.0‘, 9                     "Accept":    ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘,10                     "Accept-Language":   ‘zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3‘,11                     "Accept-Encoding":    ‘gzip, deflate‘,12                     "Connection":    ‘keep-alive‘,13                     "Upgrade-Insecure-Requests": "1",          14                 }15         16         req = urllib2.Request(url=item[‘addr‘], headers=headers) 17         res = urllib2.urlopen(req)18 19         saveFilePath = os.path.join(os.path.curdir, "down_pic", item[‘name‘].split("_")[0]+"_"+str(item["picCount"]))20         if os.path.exists(saveFilePath):21             pass22         else:23             #os.mkdir(saveFilePath) #只能建單層檔案夾24             os.makedirs(saveFilePath)25         file_name = os.path.join(saveFilePath, item[‘name‘] + ‘.jpg‘)26         with open(file_name, ‘wb‘) as fp:27             fp.write(res.read())28         with open("./savePath.txt", "a") as fh:29             fh.write(file_name+"\n")

 用urllib2抓取圖片的時候,部分request報HTTP Error 400: Bad Request

參照:https://stackoverflow.com/questions/8840303/urllib2-http-error-400-bad-request?answertab=active#tab-top

because you aren‘t escaping the string for a URL.

改用requests成功解決問題。

 1 import os 2 import requests  3  4  def process_item(self, item, spider): 5         headers = { 6                     "Host":    ‘img31.mtime.cn‘, 7                     "User-Agent":    ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:53.0) Gecko/20100101 Firefox/53.0‘, 8                     "Accept":    ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘, 9                     "Accept-Language":   ‘zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3‘,10                     "Accept-Encoding":    ‘gzip, deflate‘,11                     "Connection":    ‘keep-alive‘,12                     "Upgrade-Insecure-Requests": "1",          13                 }14         15         res = requests.get(item[‘addr‘], headers=headers)16         #print res17         #print res.url18         #print res.headers19         #print res.content 位元據20         21         saveFilePath = os.path.join(os.path.curdir, "down_pic", item[‘name‘].split("_")[0]+"_"+str(item["picCount"]))22         if os.path.exists(saveFilePath):23             pass24         else:25             #os.mkdir(saveFilePath) #只能建單層檔案夾26             os.makedirs(saveFilePath)27         file_name = os.path.join(saveFilePath, item[‘name‘] + ‘.jpg‘)28         with open(file_name, ‘wb‘) as fp:29             fp.write(res.content)30         with open("./savePath.txt", "a") as fh:31             fh.write(file_name+"\n")

 

urllib2.HTTPError: HTTP Error 400: Bad Request

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.