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

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)
相关推荐
2501_948120156 小时前
教育资源网站的爬虫采集与个性化学习推荐
爬虫·学习
2501_9481201512 小时前
深度学习在爬虫图片数据内容识别中的应用
人工智能·爬虫·深度学习
爱写bug的野原新之助12 小时前
协程爬虫案例: 王者荣耀英雄皮肤图片爬取
爬虫
煤炭里de黑猫12 小时前
Python爬虫开发实战指南:从基础到高级工具应用
人工智能·爬虫
深蓝电商API12 小时前
Selenium Grid分布式执行爬虫任务
爬虫·python·selenium
天天进步201512 小时前
生产级部署:如何结合 Docker 快速上线你的 Botasaurus 爬虫服务
爬虫·云原生
深蓝电商API13 小时前
Selenium结合Chrome DevTools协议加速爬取
爬虫·python·selenium·测试工具·chrome devtools
煤炭里de黑猫13 小时前
Python 爬虫进阶:利用 Frida 逆向移动端 App API 以实现高效数据采集
开发语言·爬虫·python
喵手1 天前
Python爬虫零基础入门【第七章:动态页面入门(Playwright)·第3节】优先 API:用 Network 找接口,回到 Requests(更稳定)!
爬虫·python·playwright·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·优先 api
喵手1 天前
Python爬虫零基础入门【第六章:增量、去重、断点续爬·第3节】幂等去重:同一条数据反复跑也不会重复入库!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·增量、去重、断点续爬·幂等去重