python是什么虫子_python蠕虫

python爬c

coding: utf-8

author

= ‘zhy’

import requests

from bs4 import BeautifulSoup

import multiprocessing

import time

success_num = 0

CONSTANT = 0

def getProxyIp():

global CONSTANT

proxy = []

for i in range(1, 12):

print(i)

header = {‘User-Agent’: 'Mozilla/5.0 (Windows NT 6.1) ’

'AppleWebKit/537.36 (KHTML, like Gecko) ’

'Chrome/78.0.3904.108 ’

‘Safari/537.36’}

r = requests.get(‘http://www.xicidaili.com/nt/{0}’.format(i), headers=header)

html = r.text

soup = BeautifulSoup(html)

table = soup.find(‘table’, attrs={‘id’: ‘ip_list’})

tr = table.find_all(‘tr’)[1:]

# 解析得到代理ip的地址,端口,和类型

for item in tr:

tds = item.find_all('td')

print(tds[1].get_text())

temp_dict = {}

kind = tds[5].get_text().lower()

# exit()

if 'http' in kind:

temp_dict['http'] = "http://{0}:{1}".format(tds[1].get_text(), tds[2].get_text())

if 'https' in kind:

temp_dict['https'] = "https://{0}:{1}".format(tds[1].get_text(), tds[2].get_text())

proxy.append(temp_dict)

return proxy

def brash(proxy_dict):

header = {‘User-Agent’: 'Mozilla/5.0 (Windows NT 6.1) ’

'AppleWebKit/537.36 (KHTML, like Gecko) ’

'Chrome/78.0.3904.108 ’

‘Safari/537.36’}

try:

# http://blog.csdn.net/zhy421202048/article/details/50935685

# http://blog.csdn.net/zhy421202048/article/details/50937423

r = requests.get(“https://download.csdn.net/download/xuchuanjie123/12390843”,

headers=header, proxies=proxy_dict,)

except Exception as e:

print(“failed”)

else:

print(“successful”)

time.sleep(0.5)

return None

if

name

== ‘

main

’:

i = 0

t = 0

final = 5 # 输入数字代表要获取多少次代理ip

while t < final:

t += 1

proxies = getProxyIp() # 获取代理ip网站上的前12页的ip

# print CONSTANT

for i in range(5):

i += 1

pool = multiprocessing.Pool(processes=32)

results = []

for i in range(len(proxies)):

results.append(pool.apply_async(brash, (proxies[i],)))

for i in range(len(proxies)):

results[i].get()

pool.close()

pool.join()

i = 0

time.sleep(20)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值