ip_pools

2018-10-07  本文已影响0人  裴general
import json
import time
import random
import redis
import requests
from common import ProxyGenerator


class HandleIP(object):
    def __init__(self):
        self.p = ProxyGenerator()
        self.userful_ips = []
        self.rclient = redis.Redis(host='127.0.0.1', port='6379')

    def get_ip(self):
        proxies = self.rclient.srandmember('zhongyi_ip', 1)
        return proxies

    def delete_all(self):
        self.rclient.delete('zhongyi_ip')

    def test_ip(self):
        proxy_list = self.p.get_ips()
        self.delete_all()
        headers = {
                'Accept': 'application/json, text/javascript, */*; q=0.01',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Content-Length': '230',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'Host': 'www.zycredit.com',
                'Origin': 'http://www.zycredit.com',
                'Proxy-Connection': 'keep-alive',
                'Referer': 'http://www.zycredit.com/front/base',
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36'
                              ' (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36',
                'X-Requested-With': 'MLHttpRequest',
            }
        name = '科大讯飞股份有限公司'
        suggest = 'http://www.zycredit.com/front/base/suggest'
        company_query = 'http://www.zycredit.com/front/base/companyquery/'
        print(proxy_list)
        for proxy in proxy_list:
            proxies = {'http': proxy, 'https': proxy}
            form_data = {'keyword': name}
            try:
                response = requests.post(suggest, headers=headers, proxies=proxies, data=form_data, timeout=5)
                cookies = response.cookies.get_dict()
                info = {
                    'curPage': '1',
                    'key': name,
                    'code': '0',
                    'company': '1',
                    'name': '0',
                    'address': '0',
                    'businessScope': '0',
                    'sort': '',
                    'sortType': ''
                }
                response = requests.post(company_query, headers=headers, cookies=cookies, data=info, proxies=proxies,
                                         timeout=5)
                res = json.loads(response.text)
                if res['obj'] == '0002':
                    print('ip被封, 垃圾ip')
                else:
                    print(proxies)
                    self.rclient.sadd('zhongyi_ip', proxies)
            except Exception as e:
                print(e, '垃圾IP')


def main():
    p = HandleIP()
    while True:
        p.test_ip()
        time.sleep(180)
        p.delete_all()


if __name__ == '__main__':
    main()

上一篇下一篇

猜你喜欢

热点阅读