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

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)
相关推荐
feasibility.10 小时前
反爬十层妖塔:现代爬虫攻防的立体战争
爬虫·python·科技·scrapy·rust·go·硬件
小白学大数据13 小时前
基于大模型的Python智能爬虫:语义识别与数据清洗实践
开发语言·爬虫·python·数据分析
无限中终21 小时前
如何抓取某音视频的互动数据
爬虫·python
WL_Aurora1 天前
Python爬虫实战(一):图书网站API接口爬取
爬虫·python
川冰ICE2 天前
Python爬虫实战⑱|Pandas分组聚合,一键生成统计报表
爬虫·python·pandas
码途漫谈2 天前
Scrapling:让爬虫在现代 Web 里“活下来”的自适应抓取框架
前端·爬虫·ai·开源
Marvel__Dead3 天前
基于 AI 大模型的百度旋转验证识别(通用能力极强)
人工智能·爬虫·python·验证码识别·ai 大模型
solo_993 天前
Python 爬取小红书 文章标题和内容 仅供学习
爬虫
tang777894 天前
2026年国内代理IP服务商横向测评:企业级爬虫如何选型?
运维·服务器·网络·爬虫·python·代理
小白学大数据5 天前
面向大规模爬取:Python 全站链接爬虫优化(过滤 + 断点续爬)
开发语言·爬虫·python