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

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)
相关推荐
跨境数据猎手14 小时前
反向海淘实战|独立站搭建+国内电商API对接
开发语言·爬虫·架构
电商API_180079052471 天前
电商ERP 自动同步订单功能实现拆解与技术手段
服务器·前端·网络·爬虫
绘梨衣5471 天前
求助帖:pdf复杂表格解析
爬虫·python·pdf
huangdong_2 天前
电商图片下载工具技术路线全面解析:从爬虫方案到浏览器方案的完整技术演进与选型深度指南
爬虫
江华森3 天前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python
狗都不学爬虫_3 天前
AI逆向 - 某定制瑞树6纯算(ck-header-params后缀)
爬虫·python·网络爬虫
亿牛云爬虫专家3 天前
2026架构前沿:将Declarative Crawler(声明式爬虫)引入你的技术栈
爬虫·架构·爬虫代理·requests·隧道代理·声明式爬虫·转发模式
跨境数据猎手3 天前
反向海淘SaaS系统架构拆解与业务技术
大数据·爬虫·系统架构
跨境观察员小周3 天前
如何使用Chat GPT进行爬虫?2026年最新教程
爬虫·gpt·chatgpt
喜欢的名字被抢了4 天前
让爬虫请求像真实用户:六项反反爬策略详解
爬虫