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

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)
相关推荐
深蓝电商API9 分钟前
AI自动识别网页结构:零规则提取任意网站数据
人工智能·爬虫
小白学大数据38 分钟前
爬虫优化:Python 剔除无效超时代理实操
服务器·爬虫·python
在水一缸1 小时前
当开源硬件撞上闭源围墙:从 Flux.ai 律师函事件看 AI 时代的爬虫法律风险与技术边界
人工智能·爬虫·开源·开源硬件·数据合规·法律风险·flux.ai
周小码1 小时前
Scrapling 高效网络爬虫实战指南
爬虫
Super Scraper2 小时前
如何使用 cURL 发送 JSON:-d、--json 及常见错误的完整指南
人工智能·爬虫·python·自动化·json·mcp
电商API_180079052473 小时前
技术分享:如何实现批量自动化获取淘宝商品视频主图API
运维·爬虫·数据挖掘·自动化
创世宇图5 小时前
Scrapling 高效网络爬虫实战指南
爬虫·开源
上海云盾-小余21 小时前
业务接口防爬虫窃取:全链路鉴权与访问管控落地方案
爬虫
如烟花的信页1 天前
易盾滑块逆向分析
javascript·爬虫·python·js逆向
zkkkkkkkkkkkkk1 天前
python爬虫模拟拖动滑块缺口验证码
爬虫·python·ddddocr