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

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)
相关推荐
艺杯羹1 天前
从零搭建CSDN博客爬虫:Python爬虫+多格式导出完整教程
开发语言·爬虫·python·开源·gui·csdn
Betelgeuse761 天前
从爬虫脚本到 AI 智能体:一次数据挖掘实践的完整进化
人工智能·爬虫·数据挖掘
菩提树下的凡夫2 天前
利用Python实现获取无人机图片并自动下载保存的简易爬虫
爬虫
码界奇点2 天前
基于Python的微信公众号爬虫系统设计与实现
开发语言·爬虫·python·毕业设计·web·源代码管理
小白学大数据2 天前
抖音搜索页数据批量爬取,多关键词同步采集实现
爬虫·python·数据分析
tang777892 天前
爬虫爬公开数据被封?实测有效!从原因排查到落地解决全指南
大数据·爬虫·python·网络爬虫·ip
Jelena157795857922 天前
Python 爬虫获取淘宝商品详情(标题、主图、SKU、价格)实战指南
网络·爬虫·python
上海云盾-小余2 天前
游戏业务接口防护:防爬虫、防刷量、防恶意请求一体化方案
爬虫·游戏
星空椰3 天前
从零到实战:一套完整的 Python 爬虫技术体系(requests + BeautifulSoup + 正则 + JSON)
爬虫·python·json·beautifulsoup
zhangfeng11334 天前
合法爬虫四底线 法律边界
爬虫