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

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)
相关推荐
张小凡vip2 分钟前
python--爬虫--经验积累的遇到的坑
开发语言·爬虫·python
刘广睿1 小时前
批量截图与长图拼接:自动化工作流的三种实现方案对比
爬虫·自动化·效率工具·python3.11
深蓝电商API9 小时前
asyncio 为什么能提高采集速度?
爬虫·asyncio
跨境生态圈1 天前
2026谷歌SEO快速排名深度解析:合规起量、避坑指南与实战落地策略
数据库·人工智能·爬虫·搜索引擎·chatgpt
傻啦嘿哟1 天前
某房产平台爬虫:爬取二手房源数据,分析各城市房价走势
爬虫
深蓝电商API1 天前
Celery 调度实践
爬虫·celery
数据狐(Datafox)2 天前
京东商品列表API接口解析(附 JSON 样例)
数据库·爬虫·json
深蓝电商API2 天前
爬虫日志系统如何设计?
爬虫
2601_962300472 天前
学习Python 爬虫路线
爬虫·python·学习路线·数据解析·反爬虫
傻啦嘿哟3 天前
某招聘平台爬虫:爬取招聘岗位数据,分析各城市薪资水平
开发语言·爬虫·python