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

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 天前
集成Scrapy与异步库:Scrapy+Playwright自动化爬取动态内容
运维·爬虫·scrapy·自动化
深蓝电商API1 天前
异步爬虫的终极形态:aiohttp + asyncio 实现万级并发实践
爬虫·python·aiohttp
电商API_180079052471 天前
从客户需求到 API 落地:淘宝商品详情批量爬取与接口封装实践
大数据·人工智能·爬虫·数据挖掘
深蓝电商API1 天前
爬虫性能压榨艺术:深入剖析 Scrapy 内核与中间件优化
爬虫·scrapy
傻啦嘿哟1 天前
爬虫数据去重:BloomFilter算法实现指南
爬虫·算法
Jonathan Star2 天前
网站识别爬虫(包括以浏览器插件形式运行的爬虫)主要通过分析请求特征、行为模式等差异来区分人类用户和自动化程序
运维·爬虫·自动化
深蓝电商API3 天前
从爬虫到平台:如何把你的爬虫项目做成一个技术产品?
爬虫·信息可视化·数据挖掘
苏打水com3 天前
爬虫进阶实战:突破动态反爬,高效采集CSDN博客详情页数据
爬虫·python
小白学大数据4 天前
从携程爬取的杭州酒店数据中提取价格、评分与评论的关键信息
爬虫·python·性能优化
合作小小程序员小小店4 天前
基于可视化天气系统demo,基于python+ matplotlib+request爬虫,开发语言python,数据库无,10个可视化界面,需要的可以了联系。
开发语言·爬虫·python·matplotlib