爬虫-14-利用代理爬取数据

获取一个代理

从免费代理的网站取一条数据

例如:

免费代理 - IP海
http://www.iphai.com/free/ng

在这里插入图片描述

60.12.89.218 33920 高匿 HTTPS 中国浙江嘉兴 4.14s 34分钟前
114.99.54.65 8118 高匿 HTTPS 中国安徽安庆 0.20s 34分钟前
47.110.130.152 8080 高匿 HTTPS 中国浙江杭州 0.08s 35分钟前
221.178.232.130 8080 高匿 HTTPS 中国江苏苏州 0.09s 35分钟前
110.86.136.92 9999 高匿 HTTPS 中国福建宁德 0.17s 35分钟前
49.73.113.215 9999 高匿 HTTPS 中国江苏苏州 0.11s 35分钟前
110.86.136.154 9999 高匿 HTTPS 中国福建宁德 0.16s 35分钟前

代理的用法

在这里插入图片描述

爬取演练

关注爬取的一个时间

import requests

proxies = {"https":"60.12.89.218:33920"}

s = """User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
"""

headers = {s.strip().split(":")[0].strip():s.strip().split(":")[1].strip()}

print(s)
print(headers)

url = "https://www.360kuai.com/903454ae58cf857f2?refer_scene=&scene=&sign=360_46b9a0ef&tj_url=903454ae58cf857f2&uid=c68892e973deb25ad6f3f6f99bea6d1b"
res = requests.get(url, headers=headers, proxies=proxies)
print(res)
print(res.status_code)
print(res.content)

例子

import requests


class Spider:
    def __init__(self):
        """初始化"""
        self.star_url = "https://www.xicidaili.com/nn/"
        self.header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}

    def run(self):
        """开始爬数据"""

        # 拿到页面url
        html = self.parse_url(self.star_url)
        print(html)

        # 保存数据
        self.save_html(html)

        # 提取数据,ip,port,https
        self.get_proxie_data(html)

    def get_proxie_data(self, html):
        import re

        com = re.compile(r'<tr class="">(.*?)</tr>', re.DOTALL)
        com2 = re.compile(r'<tr class="odd">(.*?)</tr>', re.DOTALL)
        tr_list = com2.findall(html)
        print(tr_list)
        p_list = []
        for tr in tr_list:
            # print(tr)
            # print('......')
            td_com = re.compile(r'<td>(.*?)</td>')
            tdres_list = td_com.findall(tr)
            print(tdres_list)
            # ['27.152.91.83', '9999', 'HTTP', '1分钟', '19-10-11 16:21']
            ip, port, xy, *_ = tdres_list
            proxies = {xy.lower():"{}://{}:{}".format(xy.lower(),ip,port)}
            p_list.append(proxies)
            print(proxies)
            print(p_list)
            break

    def parse_url(self, url):
        """爬取数据"""
        res = requests.get(url, headers=self.header)
        return res.content.decode()

    def save_html(self, html):
        with open('a.html', 'w', encoding='utf8') as f:
            f.write(html)


o = Spider()
o.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲸鱼编程pyhui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值