吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5578|回复: 19
收起左侧

[其他原创] 新人爬虫 爬免费ip

[复制链接]
tyl75 发表于 2018-11-24 22:42
本帖最后由 tyl75 于 2018-11-24 22:44 编辑

python3 的代码 rang次数过多会被封ip 求指点优化

#coding:utf8
import requests
import re
import chardet
def get_html(page):
    html = requests.get(page).text
    return html
def find(text):
    q = re.findall('<tbody>(.*?)</tbody>',text,re.S)
    return q
def getinfo(eachclass):
    info={}
    info['ip'] = re.search('<td data-title="IP">(.*?)</td>', eachclass, re.S).group(1)
    info['PORT'] = re.search('<td data-title="PORT">(.*?)</td>', eachclass, re.S).group(1)
    info['匿名度'] = re.search(u'<td data-title="匿名度">(.*?)</td>', eachclass, re.S).group(1)
    info['类型'] =re.search(u'<td data-title="类型">(.*?)</td>', eachclass, re.S).group(1)
    info['位置'] = re.search(u'<td data-title="位置">(.*?)</td>', eachclass, re.S).group(1)
    info['响应速度'] =re.search(u' data-title="响应速度">(.*?)</td>', eachclass, re.S).group(1)
    return info
def saveinfo(classinfo):
    f = open('info.txt','a')
    for each in classinfo:
        f.writelines('ip:' + each['ip']+"   ")
        f.writelines('端口:' + each['PORT']+"   " )
        f.writelines('匿名度:' + each['匿名度']+"   " )
        f.writelines('类型:' + each['类型']+"   " )
        f.writelines('位置:' + each['位置']+"   " )
        f.writelines('响应速度:' + each['响应速度'] + '\n')
    f.close()
classinfo = []
for i in range(1,10):
    page = 'https://www.kuaIDAili.com/free/inha/'+str(i)
    text = get_html(page)
    shuaixuan = find(text)
    qiepian = str(shuaixuan).split('<tr>')
    del qiepian[0]
    for each in qiepian:
        # print each
        info =getinfo(each)
        classinfo.append(info)
saveinfo(classinfo)

免费评分

参与人数 4吾爱币 +3 热心值 +4 收起 理由
贪恋毅世的浮华 + 1 + 1 能教我下怎么爬这个网站么? http://tujixiazai.com/category/shizufangan/
b19341 + 1 + 1 热心回复!
cralyan + 1 用心讨论,共获提升!
Pear + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| tyl75 发表于 2018-11-25 11:30
degouk42 发表于 2018-11-24 23:09
两个办法
1. 限速.在 get_html 之前sleep个1秒不行就5秒.思路就是降低访问速度.
2. 代{过}{滤}理.使用不 ...

谢谢 我待会就试试 之前python2一直卡在正则中文编码匹配不到,就换了python3 请问有什么办法能匹配的上吗 \u533f\u540d\u5ea6 这种编码
degouk42 发表于 2018-11-24 23:09
两个办法
1. 限速.在 get_html 之前sleep个1秒不行就5秒.思路就是降低访问速度.
2. 代{过}{滤}理.使用不同的代{过}{滤}理ip访问页面
Pear 发表于 2018-11-24 22:50
raedon 发表于 2018-11-24 22:59
me too!
LittleOne 发表于 2018-11-24 23:20
学习了,谢谢
慕白1030 发表于 2018-11-24 23:36
来学习一下!!!
reol_233 发表于 2018-11-24 23:37
厉害了,学习一下
灬丶1丶灬 发表于 2018-11-24 23:49
学习一下
b19341 发表于 2018-11-24 23:50
学习学习!!
MODcraft 发表于 2018-11-25 00:09
感谢楼主,找了好久
学习一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-17 22:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表