当前位置:网站首页>爬虫Requests库的一些简单用法
爬虫Requests库的一些简单用法
2022-07-29 05:23:00 【赵颂@】
requests的使用官方文档
1、 和urllib是一样的 都是给HTTP发送请求
1、GET请求
python
import requests
url ="http://www.baidu.com"
r =requests.get(url=url)
带参数的GET请求,并保存
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().chrome
}
data ={
"kw":"书",
}
url ="https://tieba.baidu.com/f?"
r =requests.get(url=url,params=data)
#print(r.text)
with open("tieba.html","wb")as fp:
fp.write(r.content)
2、定制请求头
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().random
}
url ="http://www.baidu.com"
r =requests.get(url=url,headers=headers)
3、响应对像
r.text 字符串形式查看响应
r.content 字节类型查看响应
r.encoding 查看或者设置编码类型(utf8或者gbk 等)
r.status_code 查看状态码
r.headers 查看响应头部
r.url 查看请求url
r.json json格式的响应
4、POST请求
必应翻译
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().chrome
}
formdata ={
"from":"en",
"text":"dog",
"to":"zh - Hans"
}
url = "https://cn.bing.com/tlookupv3?isVertical=1&&IG=E2B1985FC4C04621828E387B864AB30C&IID=translator.5027.2"
r =requests.post(url=url,headers=headers,data=formdata)
print(r.status_code)
5、
6、
7代理
西刺代理
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().chrome
}
proxies ={
"http":"http://60.2.44.182:30963",
}
url ="https://www.baidu.com/s?ie=UTF-8&wd=ip"
r =requests.get(url=url,headers=headers,proxies=proxies)
# print(r.text)
with open("daili.html","wb")as fp:
fp.write(r.content)
8、cookie会话登录
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().chrome
}
#碰到会话的问题,就要先创建会话,,那么创建的那个会话对象就保存了cookie信息
#往下所有的操作都通过s进行访问,s.post s.get
s =requests.Session()
post_url ="http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=202043200369 "
formdata={
"icode":"",
"key_id":"1",
"email":"自己的账号",
"rkey":"1675e02911435a4867105858c862a18c",
"password":"密码",
"origURL":"http://www.renren.com/home",
"f":"https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DOyMkLmY-DmwEtjONGn3IO2CEVIEZHHjxeeHGy6z3olK%26wd%3D%26eqid%3Dceab788e00072b59000000025ebbe1a2",
"domain":"renren.com",
"captcha_type":"web_login"
}
#
r =s.post(url=post_url,headers=headers,data=formdata)
# print(r.text)
get_url ="http://www.renren.com/974397449/profile"
#s.get就会带着cookie去访问登陆后的页面
r =s.get(get_url,headers=headers)
print(r.text)
边栏推荐
- 多线程和并发
- 数论:px+py 不能表示的最大数为pq-p-q的证明
- arduino uno错误分析avrdude: stk500_recv(): programmer is not responding
- Review of neural network related knowledge (pytorch)
- FPGA based: multi-target motion detection (hand-in-hand teaching ①)
- 华为云14天鸿蒙设备开发-Day7WIFI功能开发
- Hal library learning notes-11 I2C
- 【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
- 电力电子:单项逆变器设计(MATLAB程序+AD原理图)
- STM32: mcnamu wheel tracking task (library function program code)
猜你喜欢

Hal learning notes - Basic timer of 7 timer

【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效

EPS32+Platform+Arduino 跑马灯

Reading papers on false news detection (I): fake news detection using semi supervised graph revolutionary network

ML7 self study notes

2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)

QT learning notes QT model/view

【软件工程之美 - 专栏笔记】16 | 怎样才能写好项目文档?

HAL库学习笔记-11 I2C

Logistic regression - project practice - credit card detection task (Part 2)
随机推荐
ArduinoIDE + STM32Link烧录调试
2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
CS4344国产替代DP4344 192K 双通道 24 位 DA 转换器
Migration learning robot visual domain adaptation with low rank reconstruction
基于51单片机的DAC0832波形发生器
Ml8 self study notes
2022 spring recruit - Shanghai an road FPGA post Manager (and Lexin SOC interview)
DP1332E多协议高度集成非接触式读写芯片
HAL库学习笔记- 8 串口通信之概念
Hal library learning notes-13 application of I2C and SPI
ML7 self study notes
八大排序-----------快速排序
网络爬虫
SimpleFOC调参1-力矩控制
给二维表添加时间序列索引
Hal library learning notes-14 ADC and DAC
ABSA1: Attentional Encoder Network for Targeted Sentiment Classification
Ml8 self study notes LDA principle formula derivation
STM32 串口乱码