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

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)
相关推荐
2601_951645786 小时前
如何优雅地使用c语言编写爬虫
c语言·爬虫·网络请求·字符串处理·cspider
在放️10 小时前
Python 爬虫 · 模拟浏览器跳转 - 防盗链处理
爬虫·python
数据知道14 小时前
指纹浏览器:DNS 泄漏防范与 WebRTC 本地 IP 屏蔽的底层实现
爬虫·网络协议·tcp/ip·安全·webrtc·数据采集·指纹浏览器
在放️1 天前
Python 爬虫 · PyQuery 模块基础
爬虫·python
数据知道1 天前
指纹浏览器本地存储“孤岛化”:IndexedDB、LocalStorage、SessionStorage 的安全隔离
爬虫·安全·数据采集·指纹浏览器
小白学大数据2 天前
线上故障急救:依托 OpenClaw 日志排查 403 和 503 问题
爬虫·python·selenium·数据分析
有味道的男人2 天前
利用爬虫获取中国制造网商品详情:高效采集完整方案
爬虫·制造
anew___2 天前
2026年Python爬虫技术完全指南:从入门到实战
开发语言·爬虫·python
深蓝电商API2 天前
无头浏览器性能优化:内存占用从2GB降到200MB
爬虫·性能优化
深蓝电商API2 天前
CDP协议深度解析:不通过WebDriver直接操控浏览器
爬虫