# coding = utf-

 __autor__ = 'litao'

 import urllib.request
import urllib.request
import urllib.error
import socket
import gevent
from gevent import monkey
from bs4 import BeautifulSoup
import time
import random
home = "http://www.xicidaili.com/wt/"
first_proxy_list = []
end_proxy_list = []
# proxy_support = urllib.request.ProxyHandler({"http": "http://10.10.1.10:3128", "https": "http://10.10.1.10:1080"})
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36"
}
monkey.patch_all()
def test_proxy(proxy_key):
# for i in range(len(first_proxy_list)):
# proxy_support = urllib.request.ProxyHandler({"http":proxy_list[i]})
print(proxy_key)
proxy = {"http":proxy_key}
url = "https://www.baidu.com/" proxy_support = urllib.request.ProxyHandler(proxy)
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
res = urllib.request.Request(url=url, headers=headers)
try:
response = urllib.request.urlopen(res,timeout=)
if response.code == :
end_proxy_list.append(proxy_key)
except Exception as e:
print("error:",e)
# except socket.timeout as e:
# print("This proxy is socket.timeout")
# except urllib.error.URLError as e:
# print("This proxy is timeout") def get_proxy_list():
for i in range():
url =home + str(i+)
print(url)
# proxy_support = urllib.request.ProxyHandler({"http":"123.125.5.100:3128"})
# opener = urllib.request.build_opener(proxy_support)
# urllib.request.install_opener(opener)
res = urllib.request.Request(url=url, headers=headers)
response =urllib.request.urlopen(res,timeout=).read().decode()
soup = BeautifulSoup(response,'html.parser')
print(response)
content = soup.find_all("table",attrs={"id":"ip_list"})[].find_all('tr')[:]
for i in range(len(content)):
result = content[i].find_all('td')
proxy_enum = result[].text+":"+result[].text
print(proxy_enum)
first_proxy_list.append(proxy_enum)
time.sleep(random.randint(,)) def join_gevent(first_proxy_list,gevent_list):
for i in range(len(first_proxy_list)):
gevent_list.append(gevent.spawn(test_proxy,first_proxy_list[i])) def main():
gevent_list = []
get_proxy_list()
with open("proxy_first.txt",'a',encoding='utf-8') as f:
for item in first_proxy_list:
f.write(item+'\n')
join_gevent(first_proxy_list, gevent_list)
gevent.joinall(gevent_list)
print(end_proxy_list)
with open("proxy_end.txt",'a',encoding='utf-8') as f:
for item in end_proxy_list:
f.write(item+'\n') if __name__ == "__main__":
main()

代理池抓取基础版-(python协程)--抓取网站(西刺-后期会持续更新)的更多相关文章

  1. python协程爬取某网站的老赖数据

    import re import json import aiohttp import asyncio import time import pymysql from asyncio.locks im ...

  2. Python协程爬取妹子图(内有福利,你懂得~)

    项目说明: 1.项目介绍   本项目使用Python提供的协程+scrapy中的选择器的使用(相当好用)实现爬取妹子图的(福利图)图片,这个学会了,某榴什么的.pow(2, 10)是吧! 2.用到的知 ...

  3. Python爬虫练习(多线程,进程,协程抓取网页)

    详情点我跳转 关注公众号"轻松学编程"了解更多. 一.多线程抓取网页 流程:a.设置种子url b.获取区域列表 c.循环区域列表 d.创建线程获取页面数据 e.启动线程 impo ...

  4. 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程

    点击了解更多Python课程>>> 零基础入门Python实战:四周实现爬虫网站 Django项目视频教程 适用人群: 即将毕业的大学生,工资低工作重的白领,渴望崭露头角的职场新人, ...

  5. Python协程与Go协程的区别二

    写在前面 世界是复杂的,每一种思想都是为了解决某些现实问题而简化成的模型,想解决就得先面对,面对就需要选择角度,角度决定了模型的质量, 喜欢此UP主汤质看本质的哲学科普,其中简洁又不失细节的介绍了人类 ...

  6. python协程详解,gevent asyncio

    python协程详解,gevent asyncio 新建模板小书匠 #协程的概念 #模块操作协程 # gevent 扩展模块 # asyncio 内置模块 # 基础的语法 1.生成器实现切换 [1] ...

  7. Python核心技术与实战——十六|Python协程

    我们在上一章将生成器的时候最后写了,在Python2中生成器还扮演了一个重要的角色——实现Python的协程.那什么是协程呢? 协程 协程是实现并发编程的一种方式.提到并发,肯很多人都会想到多线程/多 ...

  8. 046.Python协程

    协程 1 生成器 初始化生成器函数 返回生成器对象,简称生成器 def gen(): for i in range(10): #yield 返回便能够保留状态 yield i mygen = gen( ...

  9. Python 协程总结

    Python 协程总结 理解 协程,又称为微线程,看上去像是子程序,但是它和子程序又不太一样,它在执行的过程中,可以在中断当前的子程序后去执行别的子程序,再返回来执行之前的子程序,但是它的相关信息还是 ...

随机推荐

  1. 应用安全-CMF/CMS漏洞整理

    CMS识别 云悉 http://whatweb.bugscaner.com/batch.html CakePHP  CakePHP <= / Cache Corruption Exploit 2 ...

  2. 002/Node.js(Mooc)--Http知识

    1.什么是Http 菜鸟教程:http://www.runoob.com/http/http-tutorial.html 视频地址:https://www.imooc.com/video/6713 h ...

  3. 关于NewJson dll 引用不一致

    {System.IO.FileLoadException: 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKe ...

  4. (前篇:NIO系列 推荐阅读) Java NIO 底层原理

    出处: Java NIO 底层原理 目录 1.1. Java IO读写原理 1.1.1. 内核缓冲与进程缓冲区 1.1.2. java IO读写的底层流程 1.2. 四种主要的IO模型 1.3. 同步 ...

  5. A.Gennady and a Card Game

    http://m3.codeforces.com/contest/1097/problem/A Gennady and a Card Game time limit per test 1 second ...

  6. javascript基础总汇

    ## javaScript是什么:1.JavaScript 运行在客户端(浏览器)的编程语言2.用来给HTML网页增加动态功能3.用来给HTML网页增加动态功能.4.Netscape在最初将其脚本语言 ...

  7. Django 的 logging日志文件配置

    在Django的settings配置文件里配置以下信息: import os BASE_LOG_DIR = os.path.join(BASE_DIR , "log") # log ...

  8. ELK windows下部署测试

    操作系统 win7 (当时想在linux下部署,虚拟内存过小,转而在windows下) 版本说明 elasticsearch-6.6.2  (elasticsearch-head-master插件) ...

  9. arm算力

    arm 算力运算 MIPS: Million Instructions executed Per SecondDMIPS: Dhrystone Million Instructions execute ...

  10. bzoj3772 精神污染 dfs 序+主席树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3772 题解 很简单的一道题目. 上午研究一个题目的时候发现了这个题目是一个弱化版,所以来写了一 ...