怎样使用python爬虫获得免费代理IP

怎样使用python爬虫获得免费代理IP


爬虫一直是python使用的一个重要部分,而许多网站也为此做了许多反爬措施,其中爬虫访问过于频繁直接封ip地址也作为一种“伤敌一千,自损八百”的方法被许多网站采用,代理ip便可以防止这种情况出现。

进行爬取和测试有效性

分析完毕开始爬取ip,直接使用第三方的requestsBeautifulSoup4,可以让抓取变得很方便,代码如下:

from iptools import header, dict2proxy
from bs4 import BeautifulSoup as Soup

def parse_items(items):
    # 存放ip信息字典的列表
    ips = []
    for item in items:
        tds = item.find_all('td')
        # 从对应位置获取ip,端口,类型
        ip, port, _type = tds[1].text, int(tds[2].text), tds[5].text
        ips.append({
   'ip': ip, 'port': port, 'type': _type})
    return ips

def check_ip(ip):
    try:
        proxy = dict2proxy(ip)
        url = 'https://www.ipip.net/'
        r = requests.get(url, headers=head, proxies=pro,timeout=5)
        r.raise_for_status()
    except:
        return False
    else:
        return True

def get_proxies(index):
    url = 'http://zhimaruanjian.com// % index
    r = requests.get(url, headers=header)
    r.encoding = r.apparent_encoding
    r.raise_for_status()
    soup = Soup(r.text, 'lxml')
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值