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

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 小时前
全国微博签到数据201912-202004
大数据·爬虫·python·数据挖掘·数据分析·新浪微博
TlSfoward2 小时前
DLL 指纹库 采集、检索 TLSFOWARD tls指纹库
爬虫·网络协议·https·自动化
有味道的男人1 天前
基于 Codex 爬虫构建亚马逊无人上架系统|竞品详情采集到商品发布完整实现
爬虫·亚马逊
张小凡vip1 天前
Python爬虫实战:高效稳定的文件下载模块设计与实现
开发语言·爬虫·python
tang777892 天前
爬虫代理报错速查:407/408/409/410/503 从报错到根治(实测可用)
爬虫·爬虫代理·动态代理ip·代理ip·503·407·爬虫报错
小白学大数据2 天前
Python 爬虫实战:抓取汽车之家二手车成交价格与里程数据
开发语言·爬虫·python·汽车
张小凡vip3 天前
python--爬虫--成熟的爬虫框架Scrapy
爬虫·python·scrapy
大数据魔法师3 天前
DrissionPage 从入门到实战:一站式Python爬虫与网页自动化教程
爬虫·python·数据分析
银-豆豆3 天前
Python爬虫
开发语言·爬虫·python
XUEYUAN52123 天前
跨境爬虫与矩阵运维:代理IP风控原理、节点差异与厂商技术调研
运维·爬虫·矩阵