python爬虫入门到懵逼-2

用python2.7的urllib2 urllib发送POST请求提交表单

#--coding:utf-8--
import urllib2
import urllib
url="http://www.iqianyue.com/mypost" #带表单的网页
postdata={"name":"zzxxss","pass":"90890809"}#表单数据  name  pass
data=urllib.urlencode(postdata).encode('utf-8')#用urllib.urlencode方法进行编码处理 用encode设置为UTF8
req=urllib2.Request(url,data)#使用urllib2模块 创建request对象 参数url data
#request对象添加user-agent模拟浏览器访问
req.add_header('User-Agent',"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3141.7 Safari/537.36")
#用urllib2的urlopen()方法向服务器发请求
get_info=urllib2.urlopen(req).read()
print get_info

你可能感兴趣的:(python爬虫入门到懵逼-2)