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

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)
相关推荐
oh,huoyuyan12 小时前
火车采集器同时运行超多任务运行优化方案
爬虫·火车采集器
TlSfoward1 天前
爬虫指纹漂移监控与回归测试:JA3/JA4 变化为什么会影响线上验证 TLSFOWARD
数据库·爬虫·网络协议·搜索引擎
Albart5751 天前
Python爬虫请求头伪装后仍被反爬,基于代理池+随机延迟的绕过实战
开发语言·爬虫·python
小白学大数据2 天前
两周完成爬虫技术栈升级:Scrapy 迁移 Crawlo 的路径与取舍
爬虫·python·scrapy
深蓝电商API2 天前
用爬虫做量化交易:另类数据采集与因子挖掘
爬虫
小心我捶你啊2 天前
数据采集和Web解锁不是一回事,从用途到规则区分
前端·爬虫·网络协议
跨境观察员小周2 天前
2026 年实用指南:如何使用 XPath 进行网络爬虫与数据自动化提取?
运维·爬虫·自动化
上海云盾-小余2 天前
流量攻击详解:区分带宽耗尽与资源耗尽两类攻击
爬虫·安全·ddos
先吃饱再说2 天前
后端也能用 jQuery 爬网页?Cheerio 了解一下
爬虫·node.js·jquery
TlSfoward3 天前
从“爬虫被验证”到“验证误伤排查” TLSFOWARD抓包工具
爬虫