欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > Python爬虫技术 第27节 API和RESTful服务

Python爬虫技术 第27节 API和RESTful服务

2024/10/24 23:21:21 来源:https://blog.csdn.net/hummhumm/article/details/140837583  浏览:    关键词:Python爬虫技术 第27节 API和RESTful服务

Python 爬虫技术是一种自动化获取网页内容的方法,通常用于数据收集、信息抽取或自动化测试。在讲解 Python 爬虫技术时,我们通常会涉及到以下几个关键概念:

  1. HTTP 请求:爬虫通过发送 HTTP 请求来获取网页内容,这是爬虫与服务器交互的基础。

  2. API:API(Application Programming Interface)是应用程序编程接口,它允许不同的软件应用之间进行交互。API 通常定义了一组规则和协议,使得不同的系统能够通过 HTTP 请求和响应进行数据交换。

  3. RESTful 服务:RESTful 是一种基于 HTTP 协议的网络服务架构风格,它使用标准的 HTTP 方法(如 GET、POST、PUT、DELETE 等)来操作网络上的资源。RESTful 服务通常易于使用和扩展,因为它们遵循统一的接口。

Python 爬虫技术详解

  1. 请求网页:使用 Python 的 requests 库来发送 HTTP 请求,获取网页的 HTML 或 JSON 数据。

    import requests
    response = requests.get('http://example.com')
    
  2. 解析内容:使用 BeautifulSouplxml 等库来解析 HTML 或 XML 内容,提取所需的数据。

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(response.text, 'html.parser')
    
  3. 数据存储:将提取的数据存储到数据库或文件中,以便进一步分析或使用。

    # 假设提取的数据是一个列表
    data = [item.text for item in soup.find_all('li')]
    
  4. 处理 API:如果目标网站提供了 API,可以通过 API 直接获取数据,这通常比直接爬取网页更高效、更稳定。

    api_response = requests.get('http://api.example.com/data')
    
  5. 遵守规则:在使用爬虫技术时,需要遵守目标网站的 robots.txt 文件规定,尊重版权和隐私政策。

  6. 异常处理:编写代码时,需要考虑到网络请求可能失败的情况,并进行相应的异常处理。

    try:response = requests.get('http://example.com')response.raise_for_status()  # 检查请求是否成功
    except requests.exceptions.HTTPError as err:print(f"HTTP error occurred: {err}")
    
  7. 使用会话:对于需要多次请求同一服务器的情况,使用 requests.Session() 可以提高效率。

  8. 模拟浏览器:有时网站可能需要用户代理(User-Agent)或其他浏览器特性,可以通过设置请求头来模拟浏览器行为。

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }
    response = requests.get('http://example.com', headers=headers)
    
  9. 使用代理:在爬取过程中可能会遇到 IP 被封的情况,使用代理可以绕过这些限制。

  10. 异步请求:对于需要发送大量请求的情况,可以使用 aiohttp 等异步库来提高效率。

RESTful 服务的使用

  1. 理解资源:RESTful 服务通常将数据表示为资源,每个资源都有一个唯一的标识符(URI)。

  2. 使用 HTTP 方法:根据需要执行的操作(如获取、创建、更新、删除)选择相应的 HTTP 方法。

    # 获取资源
    response = requests.get('http://api.example.com/resource')
    # 创建资源
    response = requests.post('http://api.example.com/resource', json=data)
    # 更新资源
    response = requests.put('http://api.example.com/resource/1', json=data)
    # 删除资源
    response = requests.delete('http://api.example.com/resource/1')
    
  3. 处理状态码:理解并处理不同的 HTTP 状态码,例如 200 表示成功,404 表示未找到,500 表示服务器错误等。

  4. 使用 JSON:RESTful 服务通常使用 JSON 作为数据交换格式,需要熟悉如何发送和解析 JSON 数据。

  5. 认证和授权:如果 RESTful 服务需要认证,可能需要在请求中包含认证信息,如 OAuth 令牌。

  6. 错误处理:正确处理 API 调用中可能出现的错误,如网络错误、数据格式错误等。

通过上述步骤,你可以构建一个基本的 Python 爬虫,或者使用 RESTful 服务来获取和操作数据。记住,爬虫的使用应遵守法律法规和网站的使用条款。

让我们通过一个具体的案例来讲解 Python 爬虫技术的应用,以及如何使用 RESTful API 服务。

案例:爬取在线商店的商品信息

假设我们想要爬取一个在线商店的商品信息,包括商品名称、价格、库存状态等。

步骤 1: 确定数据来源

首先,我们需要确定数据来源。如果商店提供了 API,我们应优先使用 API,因为它通常更稳定、更快速,并且可以减少对网站服务器的压力。

步骤 2: 注册 API 并获取密钥

许多 API 服务需要注册并获取一个 API 密钥(API Key),以验证请求的合法性。

步骤 3: 阅读 API 文档

阅读 API 提供的文档,了解如何构造请求,包括基本的 URL、支持的 HTTP 方法、请求参数、认证方式等。

步骤 4: 使用 Python 发送请求

使用 requests 库构造请求并获取数据。

import requestsapi_url = 'https://api.examplestore.com/products'
api_key = 'your_api_key_here'
headers = {'Authorization': f'Bearer {api_key}','Content-Type': 'application/json'
}# 发送 GET 请求获取商品列表
response = requests.get(api_url, headers=headers)
products = response.json()
步骤 5: 解析数据

解析返回的 JSON 数据,提取所需的商品信息。

for product in products['data']:print(f"Product Name: {product['name']}")print(f"Price: {product['price']}")print(f"In Stock: {product['in_stock']}")print("-" * 30)
步骤 6: 存储数据

将提取的数据存储到适当的格式或数据库中。

import csv# 将数据写入 CSV 文件
with open('products.csv', 'w', newline='', encoding='utf-8') as file:writer = csv.writer(file)writer.writerow(['Product Name', 'Price', 'In Stock'])for product in products['data']:writer.writerow([product['name'], product['price'], product['in_stock']])
步骤 7: 处理分页

如果 API 支持分页,需要处理分页以获取所有数据。

page = 1
while True:params = {'page': page}response = requests.get(api_url, headers=headers, params=params)page_data = response.json()if not page_data['data']:breakfor product in page_data['data']:print(f"Product Name: {product['name']}")page += 1
步骤 8: 异常处理

添加异常处理逻辑,确保程序的健壮性。

try:response = requests.get(api_url, headers=headers)response.raise_for_status()  # 检查请求是否成功
except requests.exceptions.HTTPError as err:print(f"HTTP error occurred: {err}")
except requests.exceptions.RequestException as e:print(f"Error during requests to {api_url}: {e}")
步骤 9: 遵守爬虫礼仪

确保你的爬虫遵守 robots.txt 规则,不要发送请求过于频繁,以免对服务器造成过大压力。

步骤 10: 使用代理和用户代理

如果遇到 IP 被封的情况,可以使用代理和更改用户代理。

proxies = {'http': 'http://10.10.1.10:3128','https': 'http://10.10.1.10:1080',
}
headers['User-Agent'] = 'Your Custom User Agent'

通过这个案例,你可以看到 Python 爬虫技术的实际应用,以及如何与 RESTful API 服务交互。记住,实际应用中还需要考虑更多的细节,如请求频率控制、数据的进一步清洗和分析等。

好的,让我们继续优化上述案例,使其更加健壮和高效。

优化点 1: 动态处理分页

在处理分页时,我们不仅要考虑循环获取数据,还要考虑 API 的分页参数可能有所不同,以及如何动态地处理分页。

def fetch_all_products(api_url, api_key):headers = {'Authorization': f'Bearer {api_key}','Content-Type': 'application/json'}params = {}products = []while True:response = requests.get(api_url, headers=headers, params=params)page_data = response.json()if not page_data['data']:breakproducts.extend(page_data['data'])# 动态检查API的分页参数,可能是'page'或'offset'if 'next_page' in page_data:params = {'page': page_data['next_page']}elif 'next_offset' in page_data:params['offset'] = page_data['next_offset']else:breakreturn products# 使用函数
api_key = 'your_api_key_here'
all_products = fetch_all_products(api_url, api_key)

优化点 2: 异步请求

如果 API 允许并发请求,使用异步请求可以显著提高数据获取的效率。

import asyncio
import aiohttpasync def fetch_product(session, url, headers):async with session.get(url, headers=headers) as response:return await response.json()async def fetch_all_products_async(api_url, api_key):headers = {'Authorization': f'Bearer {api_key}','Content-Type': 'application/json'}tasks = []products = []page = 1while True:url = f"{api_url}?page={page}"task = fetch_product(aiohttp.ClientSession(), url, headers)tasks.append(task)if len(tasks) >= 5:  # 限制同时进行的请求数量responses = await asyncio.gather(*tasks)products.extend([resp['data'] for resp in responses])tasks.clear()if not tasks:  # 检查是否还有下一页breakpage += 1return products# 使用异步函数
api_key = 'your_api_key_here'
loop = asyncio.get_event_loop()
all_products_async = loop.run_until_complete(fetch_all_products_async(api_url, api_key))

优化点 3: 错误重试机制

在网络请求中,引入错误重试机制可以提高爬虫的稳定性。

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retrydef requests_retry_session(retries=3,backoff_factor=0.3,status_forcelist=(500, 502, 504),session=None,
):session = session or requests.Session()retry = Retry(total=retries,read=retries,connect=retries,backoff_factor=backoff_factor,status_forcelist=status_forcelist,)adapter = HTTPAdapter(max_retries=retry)session.mount('http://', adapter)session.mount('https://', adapter)return session# 使用带重试的会话
session = requests_retry_session()
response = session.get(api_url, headers=headers)

优化点 4: 日志记录

添加日志记录可以帮助我们更好地监控爬虫的状态和调试问题。

import logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')try:response = session.get(api_url, headers=headers)response.raise_for_status()
except requests.exceptions.HTTPError as err:logging.error(f"HTTP error occurred: {err}")
except requests.exceptions.RequestException as e:logging.error(f"Error during requests to {api_url}: {e}")

优化点 5: 遵守爬虫协议

确保爬虫遵守 robots.txt 文件的规定,并设置合理的请求间隔,避免给网站服务器带来过大压力。

import time
import robotparserrp = robotparser.RobotFileParser()
rp.set_url("http://examplestore.com/robots.txt")
rp.read()if rp.can_fetch("*", api_url):  # 检查是否可以爬取API URLresponse = session.get(api_url, headers=headers)
else:logging.warning("Crawling blocked by robots.txt")time.sleep(1)  # 休息一段时间再尝试

优化点 6: 数据清洗和验证

在存储数据前,进行数据清洗和验证,确保数据的准确性和一致性。

def clean_data(product):cleaned_product = {}for key, value in product.items():if key in ['name', 'price', 'in_stock']:if key == 'price':cleaned_product[key] = float(value)  # 确保价格是数字类型elif key == 'in_stock':cleaned_product[key] = bool(value)  # 确保库存是布尔类型else:cleaned_product[key] = value.strip()  # 去除字符串两端的空白字符return cleaned_productcleaned_products = [clean_data(product) for product in all_products]

通过这些优化点,我们的爬虫不仅更加健壮和高效,而且更加专业和符合最佳实践。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com