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('---')
相关推荐
Cyber4K1 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
苍煜2 小时前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
AllData公司负责人3 小时前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
Flittly4 小时前
【LangGraph新手村系列】(5)时间旅行:浏览历史、分叉时间线与修改过去
python·langchain
2301_782040454 小时前
CSS Flex布局中如何实现导航栏与Logo的左右分布_利用justify-content- space-between
jvm·数据库·python
yaoxin5211235 小时前
400. Java 文件操作基础 - 使用 Buffered Stream I/O 读取文本文件
java·开发语言·python
智驾5 小时前
Claude Code + vscode + minimax2.7
vscode·claudecode·minimax2.7
用户8356290780515 小时前
使用 Python 自动创建 Excel 折线图
后端·python
小白学大数据6 小时前
面向大规模爬取:Python 全站链接爬虫优化(过滤 + 断点续爬)
开发语言·爬虫·python
WL_Aurora6 小时前
【每日一题】贪心
python·算法