简单总结一下目前学到的基础爬虫框架

基于Python3自带的urllib库

是其他库的基础,但实际都用其他库,更加方便快捷,这里就是个基础知识。
除了展示了一个基本框架,还加入了一点反扒方法。
try excep略

1.添加随机请求头(User-Agent)

——隐藏真实浏览器

  1. 获取网址
    import urllib.request
    import random
    url=" "
    request=urllib.request.Request(url)
  2. 增加请求头
    【不随机就在自己浏览器里检查源码复制粘贴↓】
    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”))
  3. 请求数据
    response=urllib.request.urlopen(request)
  4. 如果需要就保存到文件

2.添加IP代理

——隐藏真实IP
urllib中request的urlopen没有添加代理等其他功能,所以复杂功能就通过创建handler处理器自定义这个功能,再调用opener的open方法

  • 免费代理
    网上搜,选择高匿。通过状态码200检查是否可用
  1. 获取网址+增加请求头
  2. 创建自己的处理器
    proxy={“http”: “ip” }
    proxy_handler=urllib.request.ProxyHandler(proxy)
  3. 创建自己的openner
    opener=urllib.request.build_opener(proxy_handler)
  4. 用自己创建的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认证功能

  1. 用户名密码
    user=“admin”
    pwd=“admin123”
    nei_url=“http://192.168.179.66
  2. 创建密码管理器
    pwd_manager=urllib.request.HTTPPasswordMgrWithDefaultRealm()
    pwd_manager.add_password(None,nei_url,user,pwd)
  3. 创建认证处理器(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
  1. 代码登录
    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)
  2. 代码带着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)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值