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

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)
相关推荐
椒哥4 小时前
爬虫框架playwright使用小技巧
后端·爬虫·python
晨曦5432105 小时前
针对经济学大数据的 Python 爬虫实践指南
开发语言·爬虫·python
光算科技6 小时前
产品页不被收录的6个技术原因(非重复内容/爬虫限制类)
爬虫
~贝母~12 小时前
猿人学js逆向比赛第一届第九题
开发语言·javascript·爬虫·ecmascript
Smartdaili China14 小时前
使用 Python 抓取亚马逊产品数据: 分步指南
开发语言·爬虫·python·网络爬虫·亚马逊·抓取·爬取
电商API_180079052471 天前
API 接口:程序世界的通用语言与交互基因
爬虫·python·数据挖掘·网络爬虫
失败又激情的man1 天前
python爬虫关于多进程,多线程,协程的使用
开发语言·爬虫·python
爬虫程序猿1 天前
如何利用 Java 爬虫按关键字搜索 Amazon 商品:实战指南
java·开发语言·爬虫
烛阴2 天前
提升Web爬虫效率的秘密武器:Puppeteer选择器全攻略
前端·javascript·爬虫
代码老y2 天前
爬虫技术:数据挖掘的深度探索与实践应用
人工智能·爬虫·python·数据挖掘