搜尋

首頁  >  問答  >  主體

python - urllib2.HTTPError: HTTP Error 400: Bad Request 求帮忙

新手这里,简单的直接抓取一个页面也报错,帮忙看下

url = 'https://xueqiu.com/stock/f10/finmainindex.json'
user_agent = 'Mozilla/5.0'  
values = {'symbol' : 'SZ000001',  'page' : '1','size' : '1' }  
headers = { 'User-Agent' : user_agent }  
data = urllib.urlencode(values)  
print data
request = urllib2.Request(url, data, headers)  
print request
response = urllib2.urlopen(request)  
page = response.read() 
print page 

报错如下,

(flask)[root@centos7 xueqiu]# python xueqiu.py 
symbol=SZ000001&page=1&size=1
<urllib2.Request instance at 0x7f7e6b4f81b8>
Traceback (most recent call last):
  File "xueqiu.py", line 22, in <module>
    response = urllib2.urlopen(request)  
  File "/usr/local/pythonbrew/pythons/Python-2.7.10/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/pythonbrew/pythons/Python-2.7.10/lib/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/usr/local/pythonbrew/pythons/Python-2.7.10/lib/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/pythonbrew/pythons/Python-2.7.10/lib/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/usr/local/pythonbrew/pythons/Python-2.7.10/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/local/pythonbrew/pythons/Python-2.7.10/lib/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request
PHP中文网PHP中文网2781 天前1730

全部回覆(2)我來回復

  • PHPz

    PHPz2017-04-18 10:23:19

    雷雷

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:23:19

    使用姿勢不對歪

    回覆
    0
  • 取消回覆