黑松山资源网 Design By www.paidiu.com
1.使用 urllib2 实现
#! /usr/bin/env python # -*- coding=utf-8 -*- import urllib2 url="https://www.baidu.com" req_header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11", "Accept":"text/html;q=0.9,*/*;q=0.8", "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3", "Accept-Encoding":"gzip", "Connection":"close", "Referer":None #注意如果依然不能抓取的话,这里可以设置抓取网站的host } req_timeout = 5 req = urllib2.Request(url,None,req_header) resp = urllib2.urlopen(req,None,req_timeout) html = resp.read() print(html)
2.使用 requests 模块
(1).get请求
#-*- coding:utf-8 -*- import requests url = "https://www.baidu.com" payload = {"key1": "value1", "key2": "value2"} r = requests.get(url, params=payload) print r.text
(2).post请求
#-*- coding:utf-8 -*- import requests url1 = "http://www.exanple.com/login"#登陆地址 url2 = "http://www.example.com/main"#需要登陆才能访问的地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = requests.post(url1, data=data, headers=headers) res2 = requests.get(url2, cookies=res1.cookies, headers=headers) print res2.content#获得二进制响应内容 print res2.raw#获得原始响应内容,需要stream=True print res2.raw.read(50) print type(res2.text)#返回解码成unicode的内容 print res2.url print res2.history#追踪重定向 print res2.cookies print res2.cookies["example_cookie_name"] print res2.headers print res2.headers["Content-Type"] print res2.headers.get("content-type") print res2.json#讲返回内容编码为json print res2.encoding#返回内容编码 print res2.status_code#返回http状态码 print res2.raise_for_status()#返回错误状态码
(3).使用session对象的写法
#-*- coding:utf-8 -*- import requests s = requests.Session() url1 = "http://www.exanple.com/login"#登陆地址 url2 = "http://www.example.com/main"#需要登陆才能访问的地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } prepped1 = requests.Request("POST", url1, data=data, headers=headers ).prepare() s.send(prepped1) """ 也可以这样写 res = requests.Request("POST", url1, data=data, headers=headers ) prepared = s.prepare_request(res) # do something with prepped.body # do something with prepped.headers s.send(prepared) """ prepare2 = requests.Request("POST", url2, headers=headers ).prepare() res2 = s.send(prepare2) print res2.content """另一种写法""" #-*- coding:utf-8 -*- import requests s = requests.Session() url1 = "http://www.exanple.com/login"#登陆地址 url2 = "http://www.example.com/main"#需要登陆才能访问的页面地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = s.post(url1, data=data) res2 = s.post(url2) print(resp2.content)
3.其他的一些请求方式
> r = requests.put("http://httpbin.org/put")
> r = requests.delete("http://httpbin.org/delete")
> r = requests.head("http://httpbin.org/get")
> r = requests.options(http://httpbin.org/get)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月05日
2024年10月05日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]