macos 使用vscode 开发python 爬虫(开发二)

以下是一个示例,用于爬取一个公开的示例网站(http://books.toscrape.com),并提取书籍的标题和价格:

python 复制代码
import requests
from bs4 import BeautifulSoup

# 发起请求并获取网页内容
url = '可以用上面的链接🔗'
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, 'html.parser')

# 提取书籍标题和价格
books = soup.find_all('article', class_='product_pod')

for book in books:
    title = book.h3.a['title']
    price = book.find('p', class_='price_color').text

    print('书名:', title)
    print('价格:', price)
    print('---')

这个示例使用了一个公开的书籍网站(http://books.toscrape.com),通过发送HTTP请求获取网页内容,并使用BeautifulSoup解析网页内容。然后,它找到所有的书籍信息,并提取书名和价格,并将它们打印出来。

运行脚本

powershell 复制代码
python spider.py

请确保你已经安装了所需的Python库(requests和beautifulsoup4)以及它们的依赖项。你可以使用以下命令来安装这些库:

powershell 复制代码
pip install requests beautifulsoup4

如果想要图片

powershell 复制代码
print("Runoob")

import requests
from bs4 import BeautifulSoup

# 发起请求并获取网页内容
url = '可以用上面的链接🔗'
response = requests.get(url)
html_content = response.text

# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, 'html.parser')

# 提取书籍标题和价格
books = soup.find_all('article', class_='product_pod')

for book in books:
    img_element = book.find('img', class_='thumbnail')
    img_url = img_element.get('src')
    title = book.h3.a['title']
    price = book.find('p', class_='price_color').text

    print('图片:', img_url)
    print('书名:', title)
    print('价格:', price)
    print('---')
相关推荐
wj3055853783 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李3 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154154 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君7 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG8 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户8356290780518 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL9 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
zh_xuan9 小时前
解决VS Code 控制台中文乱码
c++·vscode·乱码
猫猫的小茶馆9 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32