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

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)
相关推荐
小花皮猪7 小时前
2026 代理网络选型指南:住宅/机房/ISP/移动代理怎么选?(附 Bright Data / Oxylabs / Decodo 实测对比)
爬虫·数据采集·代理
数据狐(Datafox)9 小时前
1688商品列表API接口解析(附 JSON 样例)
java·开发语言·数据库·爬虫·json
深蓝电商API10 小时前
大语言模型能否自动完成 JS 逆向?
爬虫·js逆向
不叫猫先生10 小时前
2026 Web Scraping 代理服务商怎么选?住宅代理与代理网络对比指南
爬虫·网络代理·代理·住宅代理
SEO_juper1 天前
Java 并发编程实战:从线程基础到高并发架构
运维·人工智能·爬虫·chatgpt·seo
大数据魔法师1 天前
curl_cffi从入门到实战
爬虫·python
2601_960102041 天前
2026新破甲AI站群泛程序
爬虫·搜索引擎·蜘蛛池
深蓝电商API1 天前
AI 如何辅助编写爬虫?
爬虫
狗都不学爬虫_1 天前
AI逆向 - 天御无感点击验证(补+纯)
爬虫·python·网络爬虫
小静AI工程实验室2 天前
Python 爬虫翻页为何重复、漏数据?SQLite 复现 OFFSET、复合游标与快照的 8 项检查
爬虫·python·sqlite