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

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)
相关推荐
遇事不決洛必達38 分钟前
【爬虫随笔】常见js混淆原理和特征
javascript·爬虫·逆向·js加密
小熊Coding44 分钟前
Python二手图书市场行为分析系统
开发语言·爬虫·python·django·计算机毕业设计·数据可视化分析·二手图书分析系统
2601_9601020419 小时前
什么是蜘蛛池?免费蜘蛛池搭建软件全面科普
服务器·前端·爬虫·搜索引擎·蜘蛛池
清水白石0082 天前
从脚本到系统:设计一个支持插件、限流、重试与监控的 Python 异步爬虫框架
网络·爬虫·python
狗都不学爬虫_2 天前
JS逆向 - QY信息公示登录(加速乐+阉割版5S+瑞树+鸡眼4)
javascript·爬虫·python
盲敲代码的阿豪2 天前
Python 爬虫入门基础教程:从入门到实践
开发语言·爬虫·python
fly spider3 天前
AI 到底是怎么访问网页的?从爬虫、Browser Agent 到 Computer Use
人工智能·爬虫
Keano Reurink3 天前
SEO数据管道:用Airflow搭建自动化工作流
运维·人工智能·爬虫·搜索引擎·自动化·ai编程·seo
跨境数据猎手3 天前
代购系统技术选型全复盘:Laravel / Go / 自研 / SaaS 怎么选
爬虫·php·laravel