基于Python3自带的urllib库
是其他库的基础,但实际都用其他库,更加方便快捷,这里就是个基础知识。
除了展示了一个基本框架,还加入了一点反扒方法。
try excep略
1.添加随机请求头(User-Agent)
——隐藏真实浏览器
- 获取网址
import urllib.request
import random
url=" "
request=urllib.request.Request(url) - 增加请求头
【不随机就在自己浏览器里检查源码复制粘贴↓】
user_agent=" "
【随机的就定义一个list从中随机选↓,网上有常用user-agent汇总】
user_agent_list=[
" ",
" ",
" ",
" "
]
random_user_agent=random.choice(user_agent_list)
request.add_header(“User-Agent”,random_user_agent)
print(request.get_header(“User-agent”)) - 请求数据
response=urllib.request.urlopen(request) - 如果需要就保存到文件
2.添加IP代理
——隐藏真实IP
urllib中request的urlopen没有添加代理等其他功能,所以复杂功能就通过创建handler处理器自定义这个功能,再调用opener的open方法
- 免费代理
网上搜,选择高匿。通过状态码200检查是否可用
- 获取网址+增加请求头
略 - 创建自己的处理器
proxy={“http”: “ip” }
proxy_handler=urllib.request.ProxyHandler(proxy) - 创建自己的openner
opener=urllib.request.build_opener(proxy_handler) - 用自己创建的opener调用open方法请求数据
response=openner.open(url)
data=response.read()
print(response)
print(data)
- 付费代理
【第一种方式,与上面相比就是代理不同】
1.代理ip
money_proxy={“http”:“username:pwd@ip”}
2.代理的处理器
proxy_handler=urllib.request.ProxyHandler(money_proxy)
3.通过处理器创建opener
opener=urllib.request.build_opener(proxy_handler)
4.open发送请求
data=opener.open(url).read()
【第二种方式,使用密码管理器】
1.用户名密码
use_name=“abcname”
pwd=“1234556”
proxy_money=“123.158.63.130:8888”
#2.创建密码管理器,添加用户名和密码
password_manager=urllib.request.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None,proxy_money,use_name,pwd)
#3.创建可以验证代理ip的处理器
handler_auth_proxy=urllib.request.ProxyBasicAuthHandler(password_manager)
#4.根据处理器创建opener
opener_auth=urllib.request.build_opener(handler_auth_proxy)
#5.发送请求
response=opener_auth.open(url)
print(response.read())
3.用于内网的auth认证功能
- 用户名密码
user=“admin”
pwd=“admin123”
nei_url=“http://192.168.179.66” - 创建密码管理器
pwd_manager=urllib.request.HTTPPasswordMgrWithDefaultRealm()
pwd_manager.add_password(None,nei_url,user,pwd) - 创建认证处理器(requests用的多,但原理是这个)
auth_handler=urllib.request.HTTPBasicAuthHandler(pwd_manager)
opener=urllib.request.build_opener(auth_handler)
response=opener.open(nei_url)
print(response.read())
4.用于用户登录的cookies功能
- 手动
直接获取个人中心的页面, 手动粘贴复制PC抓包的cookies, 放在 request
1.数据url
url=" "
2.添加请求头
headers={
“User-Agent”:" ",
“Cookie”:“main[XWJOKE]=……”
}
3.构建请求对象,发送请求,读取数据,保存到文件中验证数据
request=urllib.request.Request(url,headers=headers)
response=urllib.request.urlopen(request)
data=response.read()
print(type(data))
with open(‘xxx.html’,‘wb’) as f:
f.write(data) - 自动
代码登录-登录成功:cookie(有效)-自动带着cookie去请求个人中心-cookiejar自动保存这个cookie
from http import cookiejar
from urllib import parse
- 代码登录
login_url=" "
#在登录之前找登录的参数
login_form_data={
“username”:"",
“pwd”:"",
“formhash”:"",
“backurl”:""
}
#定义有添加cookie功能的处理器,生成 opener
cook_jar=cookiejar.CookieJar()
cook_handler=urllib.request.HTTPCookieProcessor(cook_jar)
opener=urllib.request.build_opener(cook_handler)
#带着参数发送登录请求POST
#添加请求头
headers={
“User-Agent”:""
}
##1.参数将来需要转译;2.post请求的data要求是bytes
login_str=parse.urlencode(login_form_data).encode(‘utf-8’)
login_request=urllib.request.Request(login_url,headers=headers,data=login_str)
#如果登录成功,cookiejar自动保存cookie
opener.open(login_request) - 代码带着cookie去访问 个人中心
center_url=“https://www.xxxxx.com/member/”
center_request=urllib.request.Request(center_url,headers=headers)
response=opener.open(center_url)
#bytes -->str保存
data=response.read().decode(‘gbk’)
with open(‘xxxx.html’,‘w’) as f:
f.write(data)
Python网络爬虫基础
本文介绍使用Python3的urllib库进行网络数据抓取的基本方法,包括如何设置随机请求头来模仿浏览器,以及如何添加IP代理来隐藏真实IP。此外,还介绍了如何处理内网auth认证和利用cookies实现用户登录。
64万+

被折叠的 条评论
为什么被折叠?



