首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >17.获取代理ip

17.获取代理ip

作者头像
hankleo
发布2020-09-17 11:02:50
发布2020-09-17 11:02:50
34300
代码可运行
举报
文章被收录于专栏:Hank’s BlogHank’s Blog
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
复制
import redis
import telnetlib
import urllib.request
from bs4 import BeautifulSoup

r = redis.Redis(host='127.0.0.1', port=6379)

for d in range(1, 3):  # 采集1到2页
    scrapeUrl = 'http://www.xicidaili.com/nn/%d/' % d
    req = urllib.request.Request(scrapeUrl)
    req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
    response = urllib.request.urlopen(req)
    html = response.read()

    bsObj = BeautifulSoup(html, "html.parser")

    for i in range(100):
        speed = float(bsObj.select('td')[6 + i * 10].div.get('title').replace('秒', ''))
        if speed < 0.2:  # 验证速度,只要速度在0.2秒之内的
            ip = bsObj.select('td')[1 + i * 10].get_text()
            port = bsObj.select('td')[2 + i * 10].get_text()
            ip_address = 'http://' + ip + ':' + port
            try:
                telnetlib.Telnet(ip, port=port, timeout=2)  # 用telnet对ip进行验证
            except:
                print('fail')
            else:
                print('sucess:' + ip_address)
                r.sadd('ippool', ip_address)  # 可用的ip导入到redis
                f = open('proxy_list.txt', 'a')
                f.write(ip_address + '\n')
                f.close()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档