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

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)
相关推荐
TlSfoward1 小时前
TLS指纹库的数据质量与误判治理 TLSFOWARD TLS指纹库
爬虫·网络协议·http
天启HTTP3 小时前
住宅IP和机房IP有什么区别?爬虫场景实测对比
爬虫·网络协议·tcp/ip
阿图灵3 小时前
猫眼票房监控系统实战:z3 约束求解破解字体混淆反爬
爬虫·playwright·反爬·z3·猫眼票房
亿牛云爬虫专家1 天前
爬虫调度系统设计:用 Celery 实现按媒体权重动态调整的抓取频率控制
爬虫·python·隧道代理·舆情采集·媒体权重·频率控制·ip自动轮换
小白学大数据1 天前
CuPy vs Numba vs PyTorch:GPU 加速方案怎么选
人工智能·pytorch·爬虫·python
2601_966871401 天前
Python 爬虫 + 数据挖掘实战:数据抓取与深度挖掘分析全流程
爬虫·python·数据挖掘
傻啦嘿哟1 天前
王者荣耀爬虫:爬取全英雄皮肤数据,用Python做可视化分析
开发语言·爬虫·python
pass_by_Z1 天前
Python异步爬虫实战:从零到一构建高性能并发爬虫
开发语言·爬虫·python
桃花键神2 天前
Claude Code + Bright Data Web Scraping:2026 年 3 步搭建 AI 爬虫
前端·人工智能·爬虫