o(∩_∩)o设置代理访问博客(二)o(∩_∩)o

通过python脚本刷高博客的浏览量,之前已经给大家分享过。但是由于博客页面的改版,以及旧有代理网站的失效。导致原有的脚本调整后才能继续使用。
这里分享一下,更新后的脚本:

改动点:

  1. 针对CSDN新版的博客列表页面,根据article模块获取博客地址
    在这里插入图片描述

  2. 更换代理地址网站爬取。代理网站(http://www.xiladaili.com/gaoni/[2-300]/)已经废弃,现在使用新的代理网站(https://www.kuaidaili.com/free/inha/)。除了bs提取代理信息外,增加了通过正则解析JS获取代理数据
    在这里插入图片描述

  3. 增加请求头的随机设置。(更新后的代理解析代码)

实例代码:

def parseIPList(url="https://www.kuaidaili.com/free/inha/"):
    """
    获取代理地址
    :param url:代理之地的网站  # "https://www.beesproxy.com/free"  "https://proxy.mimvp.com/freeopen"
    :return: 代理的地址列表
    """
    def search_by_re(string):
        import re
        import json
        IPs = []
        pattern= re.compile( r'const fpsList = (.*?);')
        re_result = pattern.search(string)
        if not re_result:
            return IPs
        ips = json.loads(re_result.groups()[0])
        for ip_info in ips:
            IPs.append([ip_info['ip'], ip_info['port']])
        return IPs

    def search_by_bs(string):
        IPs = []
        soup = BeautifulSoup(string)
        tds = soup.find_all("td")
        ip = ''
        port = ''

        # if td.attrs.__len__() == 0:
        #     if re.search(IPRegular, td.text):
        #         # print(td.text)
        #         IPs.append(td.text.split(':'))
        # if 'free-proxylist-tbl-proxy-ip' in td.attrs :
        #     print(td.text)
        # if 'free-proxylist-tbl-proxy-port' in td.attrs :
        #     print(td.text)

        for td in tds:
            if 'data-title' not in td.attrs:
                continue
            if 'IP' == td.attrs['data-title']:
                ip = td.text
            if "PORT" == td.attrs['data-title']:
                port = td.text
                IPs.append([ip, port])
        return IPs

    url += f'{random.randint(1, 300)}/'
    headers_proxy = headers.copy()
    headers_proxy.update({"User-Agent": random.choice(user_agent)})
    del headers_proxy['Referer']
    response = requests.get(url, headers=headers_proxy)
    IPs = []
    IPs.extend(search_by_bs(response.text))
    IPs.extend(search_by_re(response.text))

    return IPs

仅注:供大家学习参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值