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

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)
相关推荐
进击的雷神2 小时前
攻克俄语编码适配与无ID字段去重:基于纯URL拼接的国际化爬虫设计
爬虫·spiderflow
JackSparrow4146 小时前
前端安全之JS混淆+请求加密+请求签名以提升爬虫难度
前端·javascript·后端·爬虫·python·安全
kisloy19 小时前
【爬虫入门第5讲】Python爬虫文本解析详解
开发语言·爬虫·python
上海云盾-小余21 小时前
CC 高频请求 + DDoS 流量攻击实战防御:全链路 WAF 流量清洗部署完整流程
网络·爬虫·安全·小程序·ddos
喜欢吃豆1 天前
Playwright 深度解析:从浏览器自动化到动态爬虫、自动化测试与 AI Agent
运维·爬虫·自动化
kisloy1 天前
【爬虫入门第10讲】Scrapy 框架深度解析(一):五大组件与中间件
爬虫·scrapy·中间件
IPdodo_1 天前
Python 接入代理IP:爬虫网络优化实战
爬虫·python·网络代理·代理ip·ipdodo
kisloy1 天前
【爬虫入门第9讲】Python爬虫浏览器自动化
爬虫·python·自动化
独行侠影a1 天前
Selenium 爬虫反爬实战:Python 模拟浏览器点击动态页面采集
爬虫·python·selenium
q567315232 天前
企业级 HTTP 代理采购选型:技术评估清单 15 项
开发语言·网络·爬虫·网络协议·http·隧道ip·代理ip