爬虫爬取豆瓣电影、价格、书名

1、爬取豆瓣电影top250

bash 复制代码
import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}

for i in range(0, 250, 25):
    print(f"--------第{i+1}到{i+25}个电影------------")
    response = requests.get(f"https://movie.douban.com/top250?start={i}", headers=headers)

    if response.ok:
        html = response.text
        soup = BeautifulSoup(html, "html.parser")
        all_titles = soup.findAll("span", attrs={"class": "title"})
        j = i
        for title in all_titles:
            title_string = title.string
            if "/" not in title_string:
                j += 1
                print(f"{j}、{title_string}")
    else:
        print("请求失败")

2、爬取价格

bash 复制代码
import requests
from bs4 import BeautifulSoup

content = requests.get("http://books.toscrape.com/").text
soup = BeautifulSoup(content, "html.parser")
# 因为价格在标签为p的里面,所以写p,它的属性为class="price_color"
all_prices = soup.findAll("p", attrs={"class": "price_color"})
print(all_prices)
for price in all_prices:
    print(price.string[2:])

3、爬取书名

bash 复制代码
import requests
from bs4 import BeautifulSoup

content = requests.get("http://books.toscrape.com/").text
soup = BeautifulSoup(content, "html.parser")
# 因为书名在h3中,又包了一层a,所以先找h3,再找a
all_titles = soup.findAll("h3")
for title in all_titles:
    all_links = title.findAll("a")
    for link in all_links:
        print(link.string)
相关推荐
DevnullCoffe1 天前
用 MCP 让 AI Agent 直接批量下载亚马逊商品图片——原理、踩坑与实现
爬虫·python·api
深蓝电商API1 天前
电商网站IP封禁绕过:代理池+流量指纹模拟的实战方案
爬虫
川冰ICE2 天前
Python爬虫实战⑳|Pandas时间序列,趋势分析一网打尽
爬虫·python·pandas
小白学大数据2 天前
Python 爬虫动态 JS 渲染与无头浏览器实战选型指南
开发语言·javascript·爬虫·python
WL_Aurora2 天前
Python爬虫实战(三):水果行情网站大规模分页爬取
爬虫·python
Pocker_Spades_A2 天前
Python快速入门专业版(五十八)——正则表达式(re):爬虫文本提取利器(从语法到实战)
爬虫·python·正则表达式
onebound_noah2 天前
1688商品获取全解析:API与爬虫双轨实战指南
大数据·数据库·爬虫
跨境数据猎手2 天前
跨境电商平台系统开发全流程
爬虫·系统架构·个人开发
深邃-3 天前
【Web安全】-BurpSutie实战讲解(2):BP代理模块,BP重放模块,BP爆破模块,BP爬虫功能,BP解码模块,BP对比模块
爬虫·计算机网络·安全·web安全·网络安全·burpsutie