自学内容网 自学内容网

python爬虫/引用requests/基本使用

1.安装requests

进入控制台使用该命令安装requests

pip3 install requests

2.对网站使用get请求

这里用对网站进行get请求,然后打印。

import requests //引用requests

response = requests.get(url='https://www.bilibili.com/')

print(response.text)

3.对网站使用post请求

这里用对网站进行post请求,然后打印。

import requests

data = {"name","测试"}
# post请求页面数据,并向页面传递数据
response = requests.patch("https://www.bilibili.com/");
print(response.text)

4.对网站使用get带参数的请求

import requests

data = {"key1":"value1","key2":"value2"}

response = requests.get("https://www.jd.com",params=data)

print(response.url)
print(response.headers)
print(response.text)

5.获取网站的动态数据

网址可以使用浏览器的F12查询图片地址

#使用requests模块可以获得整个页面数据
#如果希望获得页面中的某一个数据改如何获取
#https://i0.hdslb.com/bfs/new_dyn/b4f40893e8c7b1f07a79f92aef9551fc3546568116406964.jpg@.webp

import requests

#获得图片
response =  requests.get('https://i0.hdslb.com/bfs/new_dyn/b4f40893e8c7b1f07a79f92aef9551fc3546568116406964.jpg@.webp')
with open('tupian.webp','wb') as f:
     f.write(response.content)

6.获得本机ip

import requests


#获得JSON
response =  requests.get("http://httpbin.org/ip")
data = response.json()
print(data)
print(data['origin'])

7. 自定义头部信息

import requests
header={
    'user-agent':'haha/v'
}
response = requests.get('http://httpbin.org/get',headers = header)
print(response.headers)#获得请求头信息
print(response.request.headers)#获得响应头信息

8.查看coolkies_arg

import requests


url = 'http://httpbin.org/cookies'
cookies = dict(coolkies_arg='hello python')
response = requests.get(url=url,cookies=cookies)
print(response.text)

 9.如何查看网站头部

1.访问网站打开发F12这里使用百度演示

2.点击网络选项刷新页面

 3.找到该文件一般在第一个的位置,点击查看信息

最后一样就是我们需要的信息。


原文地址:https://blog.csdn.net/ededabo/article/details/142458565

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!