pyhon2.7 多进程爬取url返回json数据

我本次爬取的是一批物联网设备ip中的xhr返回json信息,并解析json
得到具体型号。
已有数据:
1.物联网ip和端口
2.网站登录页面F12---->XHR----Ctrl + R ----->headers,获取url

# -*- coding: utf-8 -*-
import requests
from concurrent.futures import ProcessPoolExecutor
import re
import json

urls_list = []
with open('E:/get_js/ip.txt','r') as f:
    for line in f:
        #print line,
        urls_list.append(line.replace('\n', ''))

hea = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'}
pool = ProcessPoolExecutor(20)
def request(url):
#     response = requests.get(url)
    try:
        response = requests.get('http://%s'%(url),headers = hea,timeout = 30 ,verify=False)
        return response
    except Exception as e:
        response = requests.get('https://%s'%(url),headers = hea,timeout = 30 ,verify=False)
        return response
        print e
def read_data(future,*args,**kwargs):
    response = future.result()
    response1 = response.json()
    response.encoding = 'utf-8'
#     print(response.status_code,response.url)
#     productJSON = response.json.loads(response1)
    product = response1["szDeviceName"]+'\n'
    for each in product:
    # print each
        with open('%s.txt'%'productnvr','a') as f:
            f.write((each))
def main():
    for url in urls_list:
        done = pool.submit(request,url)
        done.add_done_callback(read_data)
if __name__ == '__main__':
    main()
    pool.shutdown(wait=True)
    f.close()
    print '====sucfull===='


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值