当前位置:网站首页>Some simple uses of crawler requests Library
Some simple uses of crawler requests Library
2022-07-29 07:58:00 【Zhao [email protected]】
requests Use Official documents
1、 and urllib It's the same Is to HTTP Send a request
1、GET request
python
import requests
url ="http://www.baidu.com"
r =requests.get(url=url)
Parameterized GET request , And save
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().chrome
}
data ={
"kw":" book ",
}
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、 Custom request header
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、 Response object
r.text View the response as a string
r.content Byte type view response
r.encoding View or set the coding type (utf8 perhaps gbk etc. )
r.status_code Check the status code
r.headers View the response header
r.url View request url
r.json json Response in format
4、POST request
Must translate
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 agent
Cicci agent
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 Session login
import requests
from fake_useragent import UserAgent
headers ={
"User-Agent":UserAgent().chrome
}
# Encounter the problem of conversation , You need to create a session first ,, Then the created session object is saved cookie Information
# All operations below pass s Visit ,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":" Own account ",
"rkey":"1675e02911435a4867105858c862a18c",
"password":" 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 I'll take cookie Go to the page after login
r =s.get(get_url,headers=headers)
print(r.text)
版权声明
本文为[Zhao [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/210/202207290520358348.html
边栏推荐
- Character shader exercise
- [paper reading | cryoelectron microscope] interpretation of the new subtomogram averaging method in relion 4.0
- Amaze UI 图标查询
- Very practical shell and shellcheck
- 《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》
- Matrix decomposition and gradient descent
- An optimal buffer management scheme with dynamic thresholds paper summary
- FLink CDC 的mysql connector中,mysql的字段是varbinary, 官方
- Write some DP
- Tcp/ip five layer reference model and corresponding typical devices and IPv6
猜你喜欢

佳木斯市场监管局开展防疫防虫害专题食品安全网络培训

Jump from mapper interface to mapping file XML in idea

10 practical uses of NFT
![[experience] relevant configuration of remote connection to intranet server through springboard machine](/img/27/25301cce8a4e5fa0c91902c7b734fb.png)
[experience] relevant configuration of remote connection to intranet server through springboard machine

Up sampling deconvolution operation

CentOS deploy PostgreSQL 13

Actual measurement of boot and pH pins of buck circuit
![[lecture notes] how to do in-depth learning in poor data?](/img/7d/5767c078600bd88b7d2146069f4f40.jpg)
[lecture notes] how to do in-depth learning in poor data?

Realize the effect of changing some colors of a paragraph of text

Implementation of simple cubecap+fresnel shader in unity
随机推荐
Implementation of simple matcap+fresnel shader in unity
207.课程表
10 practical uses of NFT
在js中,0表示false,非0表示true
Jump from mapper interface to mapping file XML in idea
[note] the art of research (understand the importance of the problem)
Dynamic thresholds buffer management in a shared buffer packet switch paper summary
Up sampling deconvolution operation
黑盒测试常见错误类型说明及解决方法有哪些?
Research on autojs wechat: the final product of wechat automatic information sending robot (effective demonstration)
Popular cow G
MySQL 45 讲 | 07 行锁功过:怎么减少行锁对性能的影响?
Solve the problem that the disk is full due to large files
Access数据库引入datagridview数据后,显示错误
Unity beginner 4 - frame animation and protagonist attack (2D)
[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M
Record of problems caused by PIP upgrade damage
Convert source package to RPM package
Character shader exercise
你学习·我奖励,21天学习挑战赛 | 等你来战