基于python2.7
get与post:
url = "http://zzk.cnblogs.com" urllib.urlopen(url)----->get方法 name = urllib.urlencode({"k":"b"}) urllib.urlopen(url+name)----->pst方法
开发者工具中form表单的method选项为post,那么必须使用post方法。
urllib:
import urllib import re reponse = urllib.urlopen("https://www.baidu.com") #打开指定的网页,返回网页所有信息 reponse_code = reponse.getcode() #获取状态码 reponse_body = reponse.read() #获取网页内容 #直接保存网页地址的内容到指定的文件 save = urllib.urlretrieve("https://www.baidu.com", filename="/home/guido/python/baidu.html") images = re.findall(r"src=‘(.*?\.jpg)‘", reponse_body) #利用正则表达式匹配数据 urllib.urlretrieve(images[0], filename="/home/guido/python/baidu_images.html")
拼接链接格式
import urllib parament = urllib.urlencode({"t":"b", "w":"ios"}) url = ("http://zzk.cnblogs.com/s?"+parament) print(url) 执行结果: http://zzk.cnblogs.com/s?t=b&w=ios
urllib2:
import urllib2 url = "http://www.phpno.com"#伪造浏览器请求头 send_headers = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding":"gzip, deflate, sdch", "Accept-Language":"zh-CN,zh;q=0.8", "Cache-Control":"max-age=0", "Connection":"keep-alive", "Cookie":"ASPSESSIONIDCCTRDBQT=OJNFDDEANPLCEFLECFILODNN; Hm_lvt_39dcd5bd05965dcfa70b1d2457c6dcae=1484820976,1484821014,1484821053; Hm_lpvt_39dcd5bd05965dcfa70b1d2457c6dcae=1484821053", "Host":"www.nm3dp.com", "Referer":"https://www.baidu.com/link?url=Q_AEn1rb05AX6miw616Tx5bIWILq5K_FpUQl_eyJ7TS&wd=&eqid=cb712bbf00052caf00000003588091e9", "Upgrade-Insecure-Requests":"1", "User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" } req = urllib2.Request(url, headers=send_headers) #合并浏览器向服务器发送的内容 r = urllib2.urlopen(req) print(r.read())
时间: 2024-10-20 22:31:29