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

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)
相关推荐
深蓝电商API29 分钟前
MongoDB 存储非结构化爬虫数据最佳实践
爬虫·mongodb
喵手17 小时前
Python爬虫实战:小红书热门笔记爬虫实战 - 搜索关键词驱动的内容采集指南!
爬虫·python·爬虫实战·零基础python爬虫教学·小红书热门笔记·搜索关键词·采集小红书热门笔记数据
MadPrinter18 小时前
Python 异步爬虫实战:FindQC 商品数据爬取系统完整教程
爬虫·python·算法·自动化
喵手19 小时前
Python爬虫实战:数据质量治理实战 - 构建企业级规则引擎与异常检测系统!
爬虫·python·爬虫实战·异常检测·零基础python爬虫教学·数据质量治理·企业级规则引擎
宇擎智脑科技21 小时前
Crawl4AI:面向大语言模型的开源智能网页爬虫框架深度解析
人工智能·爬虫·语言模型
深蓝电商API1 天前
爬虫数据清洗:Pandas 处理缺失值与异常
爬虫·pandas
去码头整点薯条ing1 天前
猿人学第二届第一题【魔改标准算法】
爬虫·python
喵手1 天前
Python爬虫实战:深度索引 - 基于关键词矩阵模拟与语义打捞的语料库构建实战!
爬虫·python·爬虫实战·语料库·零基础python爬虫教学·基于关键词矩阵模拟·语义打捞
喵手1 天前
Python爬虫实战:爬取得到App电子书畅销榜 - 从零到交付的完整实战!
爬虫·python·爬虫实战·零基础python爬虫教学·爬取app电子书畅销榜·app电子书畅销榜单数据获取
tang777892 天前
深挖66免费代理网站:隐藏功能与真实体验报告
爬虫·python·网络爬虫·ip