Python之爬虫

目录

HTTP请求

HTTP:HypertextTransferProtcol 超文本传输协议

1、请求行

python 复制代码
POST/user/info?new_user=true HTTP/1.1

#资源了路径user/info 查询参数new_user=true 协议版本HTTP/1.1

2、请求头

python 复制代码
Host:www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; ×64)

#host指主机域名

python 复制代码
User-Agent:curl/7.77.0

#告知服务器客户端的相关信息

python 复制代码
Accept:*/*

#客户端想接受的响应数据是什么类型

3、请求体

python 复制代码
{"username":"刘威","email":"liuwei@hotmail.com"}

HTTP响应

python 复制代码
# 状态行
HTTP/1.1 200 OK
# 响应头
Date:Fri,27Jan 2023 02:10:50 GMT
Content-Type:text/html;charset=utf-8
# 响应体
<!DOCTYPE html>
	<head><title>首页</title></head>
	<body><h1>hello world!</h1></body>
</html>

获得页面响应

pip install requests

python 复制代码
import requests
head = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; ×64)" }
response=requests.get("http://books.toscrape.com")
if response.ok:
    print(response.text)
else:
    print("error")

伪装用户访问

python 复制代码
import requests
headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.184.400 QQBrowser/11.3.5190.400"
}
response=requests.get("https://movie.douban.com/top250",headers=headers)
print(response.text)

打包数据

pip install bs4

python 复制代码
from bs4 import BeautifulSoup
import requests

content=requests.get("https://movie.douban.com/top250").text
# 传入BeautifulSoup的构造函数里
# 解析器
soup=BeautifulSoup(content,"html.parser")
# 能根据标签、属性等找出所有符合要求的元素
all_prices=soup.findAll("span",attrs={"class","title"})
for price in all_prices:
    print(price.string) #会把标签包围的文字返回给我们

爬取豆瓣top250

python 复制代码
from bs4 import BeautifulSoup
import requests
# 伪装用户访问
headers={
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.184.400 QQBrowser/11.3.5190.400"
}
# 根据url格式进行自动翻页
for start_num in range(0,250,25):   
    response=requests.get(f"https://movie.douban.com/top250?start={start_num}",headers=headers) #我们就可以用f字符串去格式化
    html=response.text	#打包html
    soup=BeautifulSoup(html,"html.parser")	#用html方式解析
    all_title=soup.findAll("span",attrs={"class":"title"})	#限制特定条件
    for title in all_title:	#遍历所需内容
        title_string=title.string
        if "/" not in title_string:	#限制内容显示
            print(title_string)
相关推荐
2501_9481201510 小时前
教育资源网站的爬虫采集与个性化学习推荐
爬虫·学习
啊阿狸不会拉杆10 小时前
《计算机操作系统》第七章 - 文件管理
开发语言·c++·算法·计算机组成原理·os·计算机操作系统
给你一页白纸10 小时前
将分散的Pytest测试脚本统一接入测试平台:FastAPI改造方案详解
python·pytest·接口自动化·测试平台
孤狼warrior11 小时前
图像生成 Stable Diffusion模型架构介绍及使用代码 附数据集批量获取
人工智能·python·深度学习·stable diffusion·cnn·transformer·stablediffusion
s19134838482d11 小时前
javascript练习题
开发语言·javascript·ecmascript
Java程序员威哥11 小时前
SpringBoot2.x与3.x自动配置注册差异深度解析:从原理到迁移实战
java·大数据·开发语言·hive·hadoop·spring boot·后端
大哥手下留情11 小时前
Python火车票查询方法介绍
开发语言·python
lixinnnn.11 小时前
字符串拼接:Cities and States S
开发语言·c++·算法
努力毕业的小土博^_^11 小时前
【AI课程领学】第十二课 · 超参数设定与网络训练(课时1) 网络超参数设定:从“要调什么”到“怎么系统地调”(含 PyTorch 可复用模板)
人工智能·pytorch·python·深度学习·神经网络·机器学习
这是个栗子11 小时前
前端开发中的常用工具函数(二)(持续更新中...)
开发语言·前端·javascript