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

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)
相关推荐
深蓝电商API1 小时前
行为模拟的艺术:如何让爬虫的鼠标轨迹像真人
爬虫
嫂子的姐夫2 小时前
047-MD5:飞卢网
爬虫·python·js逆向·逆向
数据知道4 小时前
从Playwright到自研:构建指纹浏览器的技术栈选型与路线图
爬虫·数据采集·指纹浏览器
嫂子的姐夫4 小时前
050-wx小程序合肥住房
爬虫·python·小程序·逆向
yijianace6 小时前
Python爬虫学习记录—— BooksToScrape分页爬取与图片下载
爬虫·python
小白学大数据6 小时前
如何自动追踪 eBay 售价?Python 爬虫实战解析
开发语言·人工智能·爬虫·python
qq3621967056 小时前
AI Crawler深度解析:GPTBot/PerplexityBot/ClaudeBot爬取行为分析与优化
人工智能·爬虫
遇事不決洛必達6 小时前
【爬虫随笔】深入理解 HTTP/HTTPS 协议、接口交互与会话机制
爬虫·网络协议·http·https·session
星川皆无恙6 小时前
Python豆瓣电影数据分析可视化系统:爬虫采集+数据清洗+可视化大屏完整项目
人工智能·爬虫·python·数据分析
如烟花的信页6 小时前
某管理服务平台点选逆向分析
javascript·爬虫·python·js逆向