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多线程实现抓取网页的更多相关文章

  1. Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法

    Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7  :Apatana Studio3+ pydev2.2+python2.7      run时设置 run--&g ...

  2. python使用urllib2抓取网页

    1.使用python的库urllib2,用到urlopen和Request方法. 2.方法urlopen原形 urllib2.urlopen(url[, data][, timeout]) 其中: u ...

  3. 怎么用Python写爬虫抓取网页数据

    机器学习首先面临的一个问题就是准备数据,数据的来源大概有这么几种:公司积累数据,购买,交换,政府机构及企业公开的数据,通过爬虫从网上抓取.本篇介绍怎么写一个爬虫从网上抓取公开的数据. 很多语言都可以写 ...

  4. Python -- 网络编程 -- 抓取网页图片 -- 豆瓣妹子

    首先分析页面URL,形如http://dbmeizi.com/category/[1-14]?p=[0-476] 图片种类对应编号: 1:'性感', 2:'有沟', 3:'美腿', 4:'小露点', ...

  5. Python -- 网络编程 -- 抓取网页图片 -- 图虫网

    字符串(str)编码成字节码(bytes),字节码解码为字符串 获取当前环境编码:sys.stdin.encoding url编码urllib.parse.quote() url解码urllib.pa ...

  6. Python多线程爬虫爬取网页图片

    临近期末考试,但是根本不想复习!啊啊啊啊啊啊啊!!!! 于是做了一个爬虫,网址为 https://yande.re,网页图片为动漫美图(图片带点颜色........宅男福利 github项目地址为:h ...

  7. 《与小卡特一起学Python》Code3 抓取网页中的某个数据

    import urllib2 file = urllib2.urlopen('http://common.cnblogs.com/script/jquery.js') message = file.r ...

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

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

  9. Python 抓取网页并提取信息(程序详解)

    最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...

随机推荐

  1. 42使用NanoPiM1Plus在Android4.4.2下的录音测试

    42使用NanoPiM1Plus在Android4.4.2下的录音测试 大文实验室/大文哥壹捌陆捌零陆捌捌陆捌贰21504965 AT qq.com完成时间:2017/12/5 17:51版本:V1. ...

  2. Mac上简单常用Terminal命令

    方案1 SSH是一个非常伟大的工具,如果你要在互联网上远程连接到服务器,那么SSH无疑是最佳的候选.SSH是加密的,OpenSSH加密所有通信(包括密码),有效消除了窃听,连接劫持和其它攻击.本文将为 ...

  3. ansible 批量推送ssh秘钥

    ansible 批量推送ssh秘钥 参考:http://docs.ansible.com/ansible/authorized_key_module.html # vi /etc/ansible/ho ...

  4. SpringBoot开源项目学习总结

    一.实现日期格式数据类型的转换 首先,定义DateConverter实现Converter<String, Date>接口: package com.stevlu.common; impo ...

  5. java虚拟机(三)--HotSpot 对象

    普通对象的创建(不包括数组和class对象): 当虚拟机遇到new指令时,会在常量池中检查是否包含这个类的符号引用(全限定名),通过这个确定是否经过类加载的过程,如果true,为该 对象分配内存,对象 ...

  6. mac install telnet

    问题: -bash: telnet: command not found -bash: brew: command not found 解决: /usr/bin/ruby -e "$(cur ...

  7. wpf 自定义单选按钮 RadioButton

    新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...

  8. 洛谷 1712 BZOJ 4653 [NOI2016]区间

    [题解] 先把区间按照未离散化的长度排序,保存区间长度,然后离散化区间端点.每次把区间覆盖的点的覆盖次数加1,如果某个点被覆盖次数大于等于m,就从前往后开始删除区间直到没有一个点被覆盖的次数大于等于m ...

  9. BZOJ 4385 洛谷3594 POI2015 WIL-Wilcze doły

    [题解] 手残写错调了好久QAQ...... 洛谷的数据似乎比较水.. n个正整数!!这很重要 这道题是个类似two pointer的思想,外加一个单调队列维护当前区间内长度为d的子序列中元素之和的最 ...

  10. 分布式数据库中CAP原理(CAP+BASE)

    分布式数据库中CAP原理(CAP+BASE) 传统的ACID 1)原子性(Atomicity): 事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功. 2)一致性(Con ...