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

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)
相关推荐
电商API_180079052472 小时前
Python 实现闲鱼商品列表批量采集,接口异常重试机制搭建
大数据·开发语言·数据库·爬虫·python
绘梨衣5474 小时前
采集基类设计遇到的描述符bug
爬虫·python·bug
如烟花的信页8 小时前
*花顺cookie逆向分析
javascript·爬虫·python·js逆向
qq3621967058 小时前
Telegram APK 下载安装完整指南 — 2026年最新
android·人工智能·爬虫·chatgpt·智能手机
yijianace9 小时前
Python爬虫项目实战:从 BeautifulSoup 到 XPath
爬虫·python·beautifulsoup
金融RPA机器人丨实在智能11 小时前
工程线索工具合规避坑指南:使用开源爬虫抓取数据会触犯法规吗?实在Agent给出了安全答案
人工智能·爬虫·安全·ai·开源
去码头整点薯条ing11 小时前
某红书笔记接口逆向【x-s参数】
javascript·爬虫·python
在放️11 小时前
Python 爬虫 · XML、xpath 与 lxml 模块基础
开发语言·爬虫·python
小白学大数据11 小时前
知网数据实战:爬虫 + 网络分析打造论文关键词图谱
爬虫·python·scrapy
有味道的男人11 小时前
利用爬虫获取 1688 商品详情:高效采集完整方案(含原生爬虫风险 + Open Claw 合规替代方案
爬虫