Python爬虫传送post请求要携带哪些参数
2017-11-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
Traceback (most recent call last):
File "/home/qians2014/文档/_proj_/spider.py", line 11, in <module>
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"))
File "/home/qians2014/文档/_proj_/spider.py", line 11, in <module>
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"))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询