python requests 400错误_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

Traceback (most recent call last):

File "xueqiu.py", line 22, in

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

# coding: utf-8

import requests

session = requests.Session()

session.headers = {

'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

}

session.get('https://xueqiu.com')

r = session.get('https://xueqiu.com/stock/f10/finmainindex.json?symbol=SZ000001&page=1&size=1')

print r.text

使用姿势不对歪

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd13941.html

相关文章 Recommend

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值