【100天精通python】Day43:python网络爬虫开发_爬虫基础(urlib库、Beautiful Soup库、使用代理+实战代码)

目录

1 urlib 库

2 Beautiful Soup库

3 使用代理

3.1 代理种类 HTTP、HTTPS 和 SOCKS5

3.2 使用 urllib 和 requests 库使用代理

3.3 案例:自建代理池

4 实战 提取视频信息并进行分析


1 urlib 库

   urllib 是 Python 内置的标准库,用于处理URL、发送HTTP请求和处理网络数据。它包含多个模块,如 urllib.request 用于发送请求,urllib.parse 用于解析URL,urllib.error 用于处理异常等。

  • urllib.request:用于发送 HTTP 请求和获取响应。
  • urllib.parse:用于解析 URL,拆分和合并 URL 的各个部分。
  • urllib.error:处理异常,如连接错误、HTTP 错误等。

常用语法:

  1. 发送GET请求:
import urllib.request

url = "https://www.example.com"
response = urllib.request.urlopen(url)
content = response.read().decode("utf-8")
print(content)

2 发送POST请求:

import urllib.request

url = "https://www.example.com"
response = urllib.request.urlopen(url)
content = response.read().decode("utf-8")
print(content)

3 实战示例:

爬取网页内容:

import urllib.request

url = "https://www.example.com"
response = urllib.request.urlopen(url)
content = response.read().decode("utf-8")
print(content)

下载文件:

import urllib.request

url = "https://www.example.com/sample.pdf"
urllib.request.urlretrieve(url, "sample.pdf")
print("File downloaded.")

处理异常:

import urllib.error

try:
    response = urllib.request.urlopen("https://www.nonexistent-website.com")
except urllib.error.URLError as e:
    print("Error:", e)

解析URL:

import urllib.parse

url = "https://www.example.com/page?param1=value1&param2=value2"
parsed_url = urllib.parse.urlparse(url)
print(parsed_url.scheme)  # 输出协议部分
print(parsed_url.netloc)  # 输出域名部分
print(parsed_url.query)   # 输出查询参数部分

以上示例只是 urllib 库的一些用法。这个库非常强大,你可以在许多网络操作中使用它,包括爬虫、API调用等。在实际项目中,你可能需要处理更多的细节,如设置请求头、处理响应等。查阅官方文档可以帮助你更全面地了解 urllib 库的功能和用法。 

4 Handler 处理器和自定义 Opener:

处理器(Handler)允许你自定义请求的处理方式,以满足特定的需求。urllib.request 模块提供了一些默认的处理器,例如 HTTPHandler 和 HTTPSHandler,用于处理 HTTP 和 HTTPS 请求。你还可以通过创建自定义的 Opener 来组合不同的处理器,实现更灵活的请求配置。

自定义 Opener 示例:

import urllib.request

# 创建自定义 Opener,组合不同的处理器
opener = urllib.request.build_opener(urllib.request.HTTPSHandler())

# 使用自定义 Opener 发送请求
response = opener.open("https://www.example.com")
content = response.read().decode("utf-8")
print(content)

5 URLError 和 HTTPError

  URLErrorHTTPError 都是 urllib.error 模块中的异常类,用于处理与网络请求相关的错误情况。

  • URLError:用于捕获与URL相关的异常,如无法解析主机名、网络不可达等。
  • HTTPError:用于捕获 HTTP 错误响应,比如请求的网页不存在(404 Not Found)、服务器错误(500 Internal Server Error)等。

URLError 示例:

import urllib.error

try:
    response = urllib.request.urlopen("https://www.nonexistent-website.com")
except urllib.error.URLError as e:
    print("URLError:", e)

HTTPError 示例:

import urllib.error

try:
    response = urllib.request.urlopen("https://www.example.com/nonexistent-page")
except urllib.error.HTTPError as e:
    print("HTTPError:", e.code, e.reason)

 在示例中,e.code 是 HTTP 错误代码,e.reason 是错误原因。

总之,处理器和 Opener 允许你自定义网络请求的行为,URLErrorHTTPError 则帮助你处理请求中可能出现的错误情况。这些功能在实际网络请求和爬虫任务中都非常有用。

2 Beautiful Soup库

        Beautiful Soup 是一个用于解析HTML和XML文档的Python库,它可以从网页中提取数据,操作文档树,并帮助你浏览和搜索文档的不同部分。它能够帮助你处理标签、属性、文本内容等,使得数据提取和处理变得更加方便。

        Beautiful Soup 是一个强大的Python库,用于解析HTML和XML文档,提取其中的数据。以下是一些 Beautiful Soup 常用的语法和方法:

from bs4 import BeautifulSoup

# HTML 示例
html = """
<html>
<head>
<title>Sample HTML</title>
</head>
<body>
<p class="intro">Hello, Beautiful Soup</p>
<p>Another paragraph</p>
<a href="https://www.example.com">Example</a>
</body>
</html>
"""

# 创建 Beautiful Soup 对象
soup = BeautifulSoup(html, "html.parser")

# 节点选择器
intro_paragraph = soup.p
print("Intro Paragraph:", intro_paragraph)

# 方法选择器
another_paragraph = soup.find("p")
print("Another Paragraph:", another_paragraph)

# CSS 选择器
link = soup.select_one("a")
print("Link:", link)

# 获取节点信息
text = intro_paragraph.get_text()
print("Text:", text)

# 获取节点的属性值
link_href = link["href"]
print("Link Href:", link_href)

# 遍历文档树
for paragraph in soup.find_all("p"):
    print(paragraph.get_text())

# 获取父节点
parent = intro_paragraph.parent
print("Parent:", parent)

# 获取兄弟节点
sibling = intro_paragraph.find_next_sibling()
print("Next Sibling:", sibling)

# 使用 CSS 选择器选择多个节点
selected_tags = soup.select("p.intro, a")
for tag in selected_tags:
    print("Selected Tag:", tag)

# 修改节点文本内容
intro_paragraph.string = "Modified Text"
print("Modified Paragraph:", intro_paragraph)

# 添加新节点
new_paragraph = soup.new_tag("p")
new_paragraph.string = "New Paragraph"
soup.body.append(new_paragraph)

# 移除节点
link.extract()
print("Link Extracted:", link)

3 使用代理

3.1 代理种类 HTTP、HTTPS 和 SOCKS5

  • HTTP代理: 用于HTTP协议的代理,适用于浏览网页等HTTP请求。
  • HTTPS代理: 用于HTTPS协议的代理,能够处理加密的HTTPS请求。
  • SOCKS5代理: 更通用的代理协议,支持TCP和UDP流量,适用于各种网络请求。

  • 抓取免费代理:

    可以使用爬虫技术从免费代理网站获取代理IP和端口。

    使用付费代理:

    付费代理通常提供更稳定和更快速的连接,适用于需要高质量代理的情况。

3.2 使用 urllibrequests 库使用代理

urllib:

import urllib.request

proxy_handler = urllib.request.ProxyHandler({'http': 'http://proxy.example.com:8080'})
opener = urllib.request.build_opener(proxy_handler)
response = opener.open('https://www.example.com')

requests:

import requests

proxies = {'http': 'http://proxy.example.com:8080'}
response = requests.get('https://www.example.com', proxies=proxies)

3.3 案例:自建代理池

import requests
from bs4 import BeautifulSoup
import random

# 获取代理IP列表
def get_proxies():
    proxy_url = "https://www.example.com/proxy-list"
    response = requests.get(proxy_url)
    soup = BeautifulSoup(response.text, "html.parser")
    proxies = [proxy.text for proxy in soup.select(".proxy")]
    return proxies

# 从代理池中随机选择一个代理
def get_random_proxy(proxies):
    return random.choice(proxies)

# 使用代理发送请求
def send_request_with_proxy(url, proxy):
    proxies = {'http': proxy, 'https': proxy}
    response = requests.get(url, proxies=proxies)
    return response.text

if __name__ == "__main__":
    proxy_list = get_proxies()
    random_proxy = get_random_proxy(proxy_list)
    
    target_url = "https://www.example.com"
    response_content = send_request_with_proxy(target_url, random_proxy)
    print(response_content)

这个案例演示了如何从代理池中随机选择一个代理,并使用选定的代理发送请求。请注意,示例中的URL和方法可能需要根据实际情况进行修改。

这些概念和示例可以帮助你了解如何使用代理,从而在网络爬虫或请求中保护你的身份和数据。

4 实战 提取视频信息并进行分析

import urllib.request
from bs4 import BeautifulSoup

# 定义目标网页的 URL
url = 'https://www.example.com/videos'

# 定义代理(如果需要使用代理)
proxies = {'http': 'http://proxy.example.com:8080'}

# 发起请求,使用代理
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(req, proxies=proxies)

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

# 创建一个空的视频列表
videos = []

# 获取视频信息
video_elements = soup.find_all('div', class_='video')
for video_element in video_elements:
    title = video_element.find('h2').text
    video_link = video_element.find('a', class_='video-link')['href']
    videos.append({'title': title, 'video_link': video_link})

# 输出提取到的视频信息
for video in videos:
    print(f"Title: {video['title']}")
    print(f"Video Link: {video['video_link']}")
    print()

# 对视频信息进行分析
num_videos = len(videos)
print(f"Total Videos: {num_videos}")

        在这个实例中,我们假设目标网页包含多个视频的信息,每个视频都有标题和视频链接。我们使用 urllib 库获取网页内容,然后使用 Beautiful Soup 解析页面,从中提取视频的标题和链接。最后,我们输出提取到的视频信息并对其进行简单的分析,计算视频的数量。

        请注意,这个实例仅用于演示基本的数据提取和分析概念。在实际应用中,你可能需要根据目标网页的结构和内容,调整代码以适应实际情况。

猜你喜欢

转载自blog.csdn.net/qq_35831906/article/details/132382676