Beautiful Soup爬取数据html xml

简介

Beautiful Soup是一个Python库,用于从HTML或XML文件中提取数据。

它提供了一种简单而灵活的方式来解析和遍历HTML或XML文档,并提供了一些有用的方法来提取所需的数据。

安装

python 复制代码
pip install beautifulsoup4

使用

  1. 导入库:在Python脚本的开头,导入Beautiful Soup库。
python 复制代码
from bs4 import BeautifulSoup
  1. 读取HTML或XML文档:使用适当的方法读取HTML或XML文档,并将其存储在一个变量中。您可以从文件中读取文档,也可以直接将文档内容作为字符串传递给Beautiful Soup。
python 复制代码
# 从文件中读取HTML文档
with open('example.html', 'r') as f:
    html_doc = f.read()

或者直接传递HTML字符串

python 复制代码
html_doc = '<html><body><h1>Hello, World!</h1></body></html>'
  1. 创建Beautiful Soup对象:使用Beautiful Soup库创建一个BeautifulSoup对象,将文档内容和解析器类型作为参数传递给它。
python 复制代码
soup = BeautifulSoup(html_doc, 'html.parser')
  1. 解析和提取数据:使用Beautiful Soup提供的方法和属性,解析和提取您需要的数据。您可以使用标签名、类名、属性等方式来定位和选择元素。
python 复制代码
# 通过标签名选择元素
title = soup.h1
print(title.text)  # 输出元素文本内容

# 通过类名选择元素
paragraphs = soup.find_all('p')
for p in paragraphs:
    print(p.text)

# 通过属性选择元素
links = soup.find_all('a', href=<a href="http://example.com" class="underline" target="_blank">Click this URL</a>)
for link in links:
    print(link['href'])

举例

URL爬数据,弄两万用户左右,然后还需要follower和following的数量

https://www.personalitycafe.com/members/ .html

保存在csv中

  1. 导入所需的库:
python 复制代码
import requests
from bs4 import BeautifulSoup
import csv
  1. 发送HTTP请求并创建Beautiful Soup对象:
python 复制代码
url = <a href="https://www.personalitycafe.com/members/" class="underline" target="_blank">Click this URL</a>
response = requests.get(url)
html_doc = response.text
soup = BeautifulSoup(html_doc, 'html.parser')
  1. 解析用户列表并提取所需信息:
python 复制代码
user_list = soup.find_all('li', class_='member')

data = []
for user in user_list:
    username = user.find('a', class_='username').text
    follower_count = user.find('dd', class_='follow_count').text
    following_count = user.find('dd', class_='following_count').text
    data.append([username, follower_count, following_count])
  1. 将数据保存到CSV文件:
python 复制代码
filename = 'user_data.csv'

with open(filename, 'w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file)
    writer.writerow(['Username', 'Follower Count', 'Following Count'])
    writer.writerows(data)

print(f"数据已保存到 {filename} 文件中。")

这样,爬取到的用户数据将会保存在名为 "user_data.csv" 的CSV文件中,包括用户名、follower数量和following数量。

请注意,根据目标网站的结构和HTML标记,可能需要进一步的调整和修改代码以正确提取所需的数据。

要正确提取所需的数据,需要根据目标网站的结构和HTML标记进行进一步的调整和修改代码。

Beautiful Soup

一些常用的Beautiful Soup操作和技巧

  1. 使用标签名称提取元素:
python 复制代码
elements = soup.find_all('tag_name')
  1. 使用CSS选择器提取元素:
python 复制代码
elements = soup.select('css_selector')
  1. 提取元素的文本内容:
python 复制代码
text = element.get_text()
  1. 提取元素的属性值:
python 复制代码
attribute_value = element['attribute_name']
相关推荐
Xpower 173 分钟前
详细解释 Codex 最近一次功能更新,以及 GPT-5.6 Sol、Terra、Luna 的能力、价格、使用方式和适用场景。
人工智能·python·gpt·学习
许彰午25 分钟前
87_Python Django模型与数据库
数据库·python·django
程序员小远34 分钟前
性能测试之性能调优
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
金銀銅鐵1 小时前
[Python] 借助图形化界面探索模n运算的规律
python·数学
Hesionberger1 小时前
快速求解完全平方数的最少数量
开发语言·数据结构·python·算法·leetcode·c#
xywww1685 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
夜雪一千10 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
能有时光10 小时前
PyTorch KernelAgent 源码解读 ---(4)--- ExtractorAgent
人工智能·pytorch·python
_Jimmy_11 小时前
Python 协程库如何使用以及有哪些使用场景
python
IMPYLH11 小时前
HTML 的 <data> 元素
前端·html