当前位置:网站首页>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
边栏推荐
- CentOS deploy PostgreSQL 13
- String class
- Phased learning about the entry-level application of SQL Server statements - necessary for job hunting (I)
- For the application challenge of smart city, shengteng AI gives a new solution
- Technology sharing | quick intercom integrated dispatching system
- UPC little C's King Canyon
- State machine DP (simple version)
- Resize2fs: bad magic number in super block
- State machine DP 3D
- Record of problems caused by PIP upgrade damage
猜你喜欢

Detailed explanation of two modes of FTP

Unity beginner 4 - frame animation and protagonist attack (2D)

Sort out the two NFT pricing paradigms and four solutions on the market

Amaze UI icon query

Jianmu continuous integration platform v2.5.2 release

Tcp/ip five layer reference model and corresponding typical devices and IPv6
![[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M](/img/5e/69987afcd1e50ba37bc49441dd3a50.png)
[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M

Compare three clock circuit schemes of single chip microcomputer

My entrepreneurial neighbors

Excellent urban design ~ good! Design # visualization radio station will be broadcast soon
随机推荐
Pytest set (7) - parameterization
Zero technology is deeply involved in the development of privacy computing financial scenario standards of the ICT Institute
Better performance and simpler lazy loading of intersectionobserverentry (observer)
准备esp32环境
The computer system has no standard tcp/ip port processing operations
Postman interface test | JS script blocking sleep and non blocking sleep
Day 014 二维数组练习
功能自动化测试实施的原则以及方法有哪些?
[lecture notes] how to do in-depth learning in poor data?
The new generation of public chain attacks the "Impossible Triangle"
Solving linear programming problems based on MATLAB
在一个sql文件中,上面定义一个测试表及数据,下面可以select* from 测试表
Sqlmap (SQL injection automation tool)
[cryoelectron microscope] relion4.0 pipeline command summary (self use)
Amaze UI icon query
Measured waveform of boot capacitor short circuit and open circuit of buck circuit
C language data type
Technology sharing | quick intercom integrated dispatching system
File system I
[freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)