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

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)
相关推荐
云动雨颤6 分钟前
爬虫是怎么工作的?从原理到用途
爬虫·python·数据挖掘
深蓝电商API37 分钟前
Scrapy源码剖析:下载器中间件是如何工作的?
爬虫·scrapy
召唤神龙19 小时前
Scrapy爬虫多IP代理配置指南:避开反爬还能提速的实战技巧
爬虫·tcp/ip
深蓝电商API20 小时前
So 库的攻防:当爬虫遇到 Native 加密函数该怎么办?
爬虫·so库
蒋星熠1 天前
爬虫中Cookies模拟浏览器登录技术详解
开发语言·爬虫·python·正则表达式·自动化·php·web
雪碧聊技术2 天前
爬豆瓣喜剧电影排行榜数据
爬虫·xhr请求
zhousenshan3 天前
python爬虫学习笔记
爬虫
causaliy3 天前
实践六:防盗链知识点——视频
爬虫·音视频
xinxinhenmeihao3 天前
爬虫导致IP被封号了如何解封?
爬虫·网络协议·tcp/ip
加油20194 天前
音视频处理(三):hls协议和m3u8详解和视频下载爬虫实战
爬虫·音视频·hls·m3u8·mpeg-2·mpeg2-ts·电视迷