python爬虫post参数会改变_python3爬虫POST传递参数问题

用户提问

Traceback (most recent call last):

File "/home/qians2014/文档/_proj_/spider.py", line 11, in

response = urllib.request.urlopen(req)

File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen

return opener.open(url, data, timeout)

File "/usr/lib/python3.3/urllib/request.py", line 471, in open

req = meth(req)

File "/usr/lib/python3.3/urllib/request.py", line 1183, in do_request_

raise TypeError(msg)

TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

这个是run以后的问题。求解。下面贴上我的代码。用的python3.3 urllib写的。

import urllib.request

import urllib.parse

url = "http://xscj.hit.edu.cn/hitjwgl/xs/jscx_all.asp"

user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/30.0.1599.114 Chrome/30.0.1599.114 Safari/537.36'

values = {'Submit':'%C8%AB%B2%BF%E4%AF%C0%C0','ZC':'11','XQ':'%D2%BB%D0%A3%C7%F8'}

headers = {'User-Agent':user_agent}

data = urllib.parse.urlencode(values)

req = urllib.request.Request(url,data,headers)

response = urllib.request.urlopen(req)

the_page = response.read()

print(the_page.decode("utf8"))

推荐答案

data = urllib.parse.urlencode(values).encode(encoding='UTF8')

试下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值