用户提问
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')
试下。