python多线程实现抓取网页
2023-09-27 14:27:21 时间
Python实现抓取网页
以下的Python抓取网页的程序比較0基础,仅仅能抓取第一页的url所属的页面,仅仅要预定URL足够多。保证你抓取的网页是无限级别的哈,以下是代码:
##coding:utf-8
'''
无限抓取网页
@author wangbingyu
@date 2014-06-26
'''
import sys,urllib,re,thread,time,threading
'''
创建下载线程类
'''
class download(threading.Thread):
def __init__(self,url,threadName):
threading.Thread.__init__(self,name=threadName)
self.thread_stop = False
self.url = url
def run(self):
while not self.thread_stop:
self.list = self.getUrl(self.url)
self.downloading(self.list)
def stop(self):
self.thread_stop = True
def downloading(self,list):
try:
for i in range(len(list) - 1):
urllib.urlretrieve(list[i],'E:\upload\download\%s.html' % time.time())
except Exception,ex:
print Exception,'_upload:',ex
def getUrl(self,url):
result = []
s = urllib.urlopen(url).read();
ss = s.replace(' ','')
urls=re.findall('<a.*?href=.*?<\/a>',ss,re.I)
for i in urls:
tmp = i.split('"')
try:
if tmp[1]:
if re.match(r'\http://.*',tmp[1]):
result.append(tmp[1])
except Exception,ex:
print Exception,":getUrl",ex
return result
if __name__ == '__main__':
list = ['http://www.baidu.com','http://www.qq.com','http://www.taobao.com','http://www.sina.com.cn']
for i in range(len(list)):
#print list[i]
download(list[i],'thread%s' % i).start()
#list = ['http://www.baidu.com','http://www.sina.com.cn']
#obj = download('http://www.baidu.com','threadName')
#obj.start();
input()
相关文章
- Python 标准库 functools 模块详解
- 关于python的二维数组
- python进阶十_正則表達式(一)
- Python获取同目录下json文件内容
- Python单元测试框架 unittest详解
- Python多线程中比time.sleep更好用的暂停方法
- Python开发多进程、多线程以及协程是什么?项目是否用?
- python基础——使用__slots__
- 158 python网络编程 - 多线程服务器
- 120 python高级 - 多线程共享全局变量
- 119 python高级 - 多线程threading(二)
- 《Python数据挖掘:概念、方法与实践》一 第2章 关联规则挖掘
- 《精通Python网络爬虫:核心技术、框架与项目实战》——1.3 网络爬虫的组成
- Python网络编程中的服务器架构(负载均衡、单线程、多线程和同步、异步等)
- python: 多线程实现的两种方式及让多条命令并发执行
- Python爬虫系列(三)多线程爬取斗图网站(皮皮虾,我们上车)
- Python爬虫技术--基础篇--多线程和ThreadLocal
- 【Python】url编码/解码/转义
- 【Python】多线程
- 聊聊Python中的多进程和多线程
- Win7下 Python中文正则的奇异表现
- 《“笨办法”学Python(第3版)》——习题6 字符串和文本
- 「基于Python技术的智慧中医商业项目」Django后端新闻应用设计
- python如何把嵌套列表转变成普通列表
- Python游戏server开发日记(二)绕过GIL启动多线程Python环境
- python中的反射
- python:多线程
- python:使用多线程同时执行多个函数
- PySpider python 爬虫
- 如何在python中构造时间戳参数
- Python之多线程和多进程
- Python攻防之多线程、C段扫描和数据库编程