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

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)
相关推荐
B站计算机毕业设计之家8 小时前
基于大数据热门旅游景点数据分析可视化平台 数据大屏 Flask框架 Echarts可视化大屏
大数据·爬虫·python·机器学习·数据分析·spark·旅游
大数据魔法师9 小时前
昆明天气数据分析与挖掘(一)- 昆明天气数据采集
爬虫·网络爬虫
光羽隹衡10 小时前
Python中的网络爬虫
开发语言·爬虫·python
深蓝电商API12 小时前
企业级爬虫架构设计:任务调度、容错、重试、降重
开发语言·爬虫·ruby
盼哥PyAI实验室13 小时前
12306反反爬虫策略:Python网络请求优化实战
网络·爬虫·python
嫂子的姐夫16 小时前
004-MD5_易车网
爬虫·python·逆向·加密
深蓝电商API17 小时前
爬虫自动化测试:Pytest + Allure 漂亮报告生成
爬虫
深蓝电商API21 小时前
爬虫+消息队列:RabbitMQ vs Kafka vs RocketMQ选型
爬虫·kafka·rabbitmq
TTGGGFF1 天前
爬虫专栏:破解网站检测selenium反爬——“当前环境正在被调试“”
爬虫·selenium·测试工具
Data_agent1 天前
京东商品视频API,Python请求示例
java·开发语言·爬虫·python