京东优惠券网站建设,wap网站为什么没有了,吉林省四平市建设局网站,网站建设流程及细节目录
1 urlib 库
2 Beautiful Soup库
3 使用代理
3.1 代理种类 HTTP、HTTPS 和 SOCKS5
3.2 使用 urllib 和 requests 库使用代理
3.3 案例#xff1a;自建代理池
4 实战 提取视频信息并进行分析 1 urlib 库 urllib 是 Python 内置的标准库#xff0c;用于处理URL、发送…目录
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 用于解析URLurllib.error 用于处理异常等。 urllib.request用于发送 HTTP 请求和获取响应。urllib.parse用于解析 URL拆分和合并 URL 的各个部分。urllib.error处理异常如连接错误、HTTP 错误等。 常用语法
发送GET请求
import urllib.requesturl https://www.example.com
response urllib.request.urlopen(url)
content response.read().decode(utf-8)
print(content)2 发送POST请求
import urllib.requesturl https://www.example.com
response urllib.request.urlopen(url)
content response.read().decode(utf-8)
print(content)3 实战示例
爬取网页内容
import urllib.requesturl https://www.example.com
response urllib.request.urlopen(url)
content response.read().decode(utf-8)
print(content)下载文件
import urllib.requesturl https://www.example.com/sample.pdf
urllib.request.urlretrieve(url, sample.pdf)
print(File downloaded.)处理异常
import urllib.errortry:response urllib.request.urlopen(https://www.nonexistent-website.com)
except urllib.error.URLError as e:print(Error:, e)解析URL
import urllib.parseurl https://www.example.com/page?param1value1param2value2
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 URLError 和 HTTPError 都是 urllib.error 模块中的异常类用于处理与网络请求相关的错误情况。 URLError用于捕获与URL相关的异常如无法解析主机名、网络不可达等。HTTPError用于捕获 HTTP 错误响应比如请求的网页不存在404 Not Found、服务器错误500 Internal Server Error等。 URLError 示例
import urllib.errortry:response urllib.request.urlopen(https://www.nonexistent-website.com)
except urllib.error.URLError as e:print(URLError:, e)HTTPError 示例
import urllib.errortry: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 允许你自定义网络请求的行为URLError 和 HTTPError 则帮助你处理请求中可能出现的错误情况。这些功能在实际网络请求和爬虫任务中都非常有用。 2 Beautiful Soup库 Beautiful Soup 是一个用于解析HTML和XML文档的Python库它可以从网页中提取数据操作文档树并帮助你浏览和搜索文档的不同部分。它能够帮助你处理标签、属性、文本内容等使得数据提取和处理变得更加方便。 Beautiful Soup 是一个强大的Python库用于解析HTML和XML文档提取其中的数据。以下是一些 Beautiful Soup 常用的语法和方法
from bs4 import BeautifulSoup# HTML 示例
html
html
head
titleSample HTML/title
/head
body
p classintroHello, Beautiful Soup/p
pAnother paragraph/p
a hrefhttps://www.example.comExample/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 使用 urllib 和 requests 库使用代理
urllib
import urllib.requestproxy_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 requestsproxies {http: http://proxy.example.com:8080}
response requests.get(https://www.example.com, proxiesproxies)3.3 案例自建代理池
import requests
from bs4 import BeautifulSoup
import random# 获取代理IP列表
def get_proxies():proxy_url https://www.example.com/proxy-listresponse 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, proxiesproxies)return response.textif __name__ __main__:proxy_list get_proxies()random_proxy get_random_proxy(proxy_list)target_url https://www.example.comresponse_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, proxiesproxies)# 解析网页内容
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).textvideo_link video_element.find(a, class_video-link)[href]videos.append({title: title, video_link: video_link})# 输出提取到的视频信息
for video in videos:print(fTitle: {video[title]})print(fVideo Link: {video[video_link]})print()# 对视频信息进行分析
num_videos len(videos)
print(fTotal Videos: {num_videos})在这个实例中我们假设目标网页包含多个视频的信息每个视频都有标题和视频链接。我们使用 urllib 库获取网页内容然后使用 Beautiful Soup 解析页面从中提取视频的标题和链接。最后我们输出提取到的视频信息并对其进行简单的分析计算视频的数量。 请注意这个实例仅用于演示基本的数据提取和分析概念。在实际应用中你可能需要根据目标网页的结构和内容调整代码以适应实际情况。