查找特定标签
Python 爬取网页详细教程
Python 是一种广泛使用的高级编程语言,其强大的库和框架使得它在数据处理、机器学习等领域具有无与伦比的优势,Python 的 requests 库和 BeautifulSoup 库是进行网页抓取的首选工具。
安装必要的库
确保你的环境中已经安装了 requests 和 beautifulsoup4 库,可以使用以下命令来安装它们:
pip install requests beautifulsoup4
使用 requests 发送 HTTP 请求
requests 提供了一个简洁易用的方式来发送HTTP请求,我们可以获取网页的内容:
import requests url = 'https://www.example.com' response = requests.get(url) print(response.status_code) print(response.text)
使用 BeautifulSoup 解析 HTML
如果网页的HTML结构复杂,我们可能需要使用 BeautifulSoup 来解析这些复杂的结构,以下是一个简单的例子:
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
for link in soup.find_all('a'):
print(link['href'])
# 获取文本
for paragraph in soup.find_all(['h1', 'p']):
print(paragraph.get_text())
处理动态加载的页面
对于包含JavaScript的网站,我们需要使用Selenium这样的工具来模拟浏览器行为,或者使用像 scrapy 这样的更强大的爬虫框架。
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://example.com")
# 找到元素并提取信息
elements = driver.find_elements_by_css_selector(".data")
for element in elements:
data = element.get_attribute("innerHTML")
print(data)
driver.quit()
数据保存和分析
最后一步是将数据保存下来,并对数据进行进一步的处理或分析,可以使用CSV文件或其他格式存储数据,然后利用 pandas 或其他数据分析库来进行处理。
import csv
with open('output.csv', mode='w') as file:
writer = csv.writer(file)
for item in items:
writer.writerow(item)
# 使用pandas读取csv文件
import pandas as pd
df = pd.read_csv('output.csv')
print(df.head())
通过以上步骤,你可以轻松地使用 Python 爬取网页内容,并对其进行分析和处理,这个过程不仅适用于静态网页,也包括那些动态加载内容的网站。

上一篇