python实例代码爬虫_python 网络爬虫实例代码
本节内容:
python 网络爬虫代码。
一共两个文件,一个是toolbox_insight.py,是一个工具文件另一个是test.py,是一个用到toolbox_insight.py中工具的测试文件 代码示例:
#filename: toolbox_insight.py
from sgmllib import sgmlparser
import threading
import time
import urllib2
import stringio
import gzip
import string
import os
#rewrite sgmlparser for start_a
class basegeturls(sgmlparser): #这个basegeturls类作用是分析下载的网页,把网页中的所有链接放在self.url中。
def reset(self):
self.url = []
sgmlparser.reset(self)
def start_a(self, attrs):
href = [v for k, v in attrs if k == 'href']
if href:
self.url.extend(href)
#for quickly finding
class newlist(list):#这个类其实是一个添加了find方法的list。当num变量在list中,返回true,当不在list中,返回false并把num按二分法插入list中
def find(self, num):
l = len(self)
first = 0
end = l - 1
mid = 0
if l == 0:
self.insert(0,num)
return false
while first < end:
mid = (first + end)/2
if num > self[mid]:
first = mid + 1
elif num < self[mid]:
end = mid - 1
else:
break
if first == end:
if self[first] > num:
self.insert(first, num)
return false
elif self[first] < num:
self.insert(first + 1, num)
return false
else:
return true
elif first > end:
self.insert(first, num)
return false
else:
return true
#下面的reptile顾名思义是一个爬虫
class reptile(threading.thread):
#name: 是爬虫是名字,queue是任务队列,所有的爬虫共用同一个任务队列
#从中取出一个任务项进行运行,每个任务项是一个要下载网页的url
#result: 也是一个队列,将下载的网页中包含的url放入该队列中
#inittime: 在本程序中没有用,只是一个为了以后扩展用的
#downloadway:是下载的网页存放的路径
#configfile: 是配置文件,存放网页的url和下载下后的路径
#maxnum: 每个爬虫有个最大下载量,当下载了这么多网页后,爬虫dead
def __init__(self, name, queue, result, flcok, inittime = 0.00001, downloadway = 'd:\\bbs\\',configfile = 'd:\\bbs\\conf.txt', maxnum = 10000):
threading.thread.__init__(self, name = name)
self.queue = queue
self.result = result
self.flcok = flcok
self.inittime = inittime
self.mainway = downloadway
self.configfile = configfile
self.num = 0 #已下载的网页个数
self.maxnum = maxnum
os.makedirs(downloadway + self.getname()) #系统调用:在存放网页的文件夹中创建一个以该爬虫name为名字的文件夹
self.way = downloadway + self.getname() + '\\'
def run(self):
opener = urllib2.build_opener() #创建一个开启器
while true:
url = self.queue.get() #从队列中取一个url
if url == none: #当取得一个none后表示爬虫结束工作,用于外部方便控制爬虫的生命期
break
parser = basegeturls() #创建一个网页分析器
request = urllib2.request(url) #网页请求
request.add_header('accept-encoding', 'gzip')#下载的方式是gzip压缩后的网页,gzip是大多数服务器支持的一种格式
try: #这样可以减轻网络压力
page = opener.open(request)#发送请求报文
if page.code == 200: #当请求成功
predata = page.read() #下载gzip格式的网页
pdata = stringio.stringio(predata)#下面6行是实现解压缩
gzipper = gzip.gzipfile(fileobj = pdata)
try:
data = gzipper.read()
except(ioerror):
print 'unused gzip'
data = predata#当有的服务器不支持gzip格式,那么下载的就是网页本身
try:
parser.feed(data)#分析网页
except:
print 'i am here'#有的网页分析不了,如整个网页就是一个图片
for item in parser.url:
self.result.put(item)#分析后的url放入队列中
way = self.way + str(self.num) + '.html'#下面的是网页的保存,不多说了
self.num += 1
file = open(way, 'w')
file.write(data)
file.close()
self.flcok.acquire()
confile = open(self.configfile, 'a')
confile.write( way + ' ' + url + '\n')
confile.close()
self.flcok.release()
page.close()
if self.num >= self.maxnum:#达到最大量后退出
break
except:
print 'end error'
#和爬虫一样是个线程类,作用是将爬虫中的result中存入的url加以处理。只要同一个服务器的网页
class proinsight(threading.thread):
def __init__(self, queue, list, homepage, inqueue):
threading.thread.__init__(self)
self.queue = queue#和爬虫中的result队列是同一个
self.list = list#是上面newlist的对象
self.homepage = homepage#主页
self.inqueue = inqueue#处理完后的url的去处
def run(self):
length = len(self.homepage)
while true:
item = self.queue.get()
if item == none:
break
if item[0:4] == '\r\n':
item = item[4:]
if item[-1] == '/':
item = item[:-1]
if len(item) >= len('http://') and item[0:7] == 'http://':
if len(item) >= length and item[0:length] == self.homepage:
if self.list.find(item) == false:
self.inqueue.put(item)
elif item[0:5] == '/java' or item[0:4] == 'java':
pass
else:
if item[0] != '/':
item = '/' + item
item = self.homepage + item
if self.list.find(item) == false:
self.inqueue.put(item)
主函数过程
我下载的网站是http://bbs.hit.edu.cn
开始网页是http://bbs.hit.edu.cn/mainpage.php 代码示例:
#filename:test
from toolbox_insight import *
from queue import queue
import threading
import sys
num = int(raw_input('enter the number of thread:'))
pnum = int(raw_input('enter the number of download pages:'))
mainpage = str(raw_input('the mainpage:'))
startpage = str(raw_input('start page:'))
queue = queue()
key = queue()
inqueue = queue()
list = newlist()
thlist = []
flock = threading.rlock()
for i in range(num):
th = reptile('th' + str(i), queue, key, flock)
thlist.append(th)
pro = proinsight(key, list, mainpage, inqueue)
pro.start()
for i in thlist:
i.start()
queue.put(startpage)
for i in range(pnum):
queue.put(inqueue.get())
for i in range(num):
queue.put(none)
个人觉得用wxpython来实现用户界面和用数据库知识查找url是更好的扩展方向。
python网络爬虫采集联想词实例
python博客文章爬虫实现代码
python网页爬虫程序示例代码
python 网络爬虫(经典实用型)
Python 网易新闻小爬虫的实现代码
python网络爬虫的代码
python 实现从百度开始不断搜索的爬虫
Python实现天气预报采集器(网页爬虫)的教程
python实例代码爬虫_python 网络爬虫实例代码相关推荐
- python sub 不区分大小写_Python网络爬虫入门篇
1. 预备知识 学习者需要预先掌握Python的数字类型.字符串类型.分支.循环.函数.列表类型.字典类型.文件和第三方库使用等概念和编程方法. Python入门篇:https://www.cnblo ...
- python cookies是什么_Python网络爬虫之Cookie技术(一)
原标题:Python网络爬虫之Cookie技术(一) 当我们访问一个需要用户名和密码登录的网站(例如某宝)时,只要我们注册好并成功登录后,下次访问该网站时,网站就会记住我们的登录信息,而无需重新登录. ...
- python网络爬虫_Python网络爬虫——爬取视频网站源视频!
原标题:Python网络爬虫--爬取视频网站源视频! 学习前提 1.了解python基础语法 2.了解re.selenium.BeautifulSoup.os.requests等python第三方库 ...
- python外国网站爬虫_python 网络爬虫-爬取网页外部网站
前言 上一篇中我们在维基百科的内部网站上随机跳转进入文章类网页,而忽视外部网站链接.本篇文章将处理网站的外部链接并试图收集一些网站数据.和单个域名网站爬取不同,不同域名的网站结构千差万别,这就意味我们 ...
- lofter 爬虫_Python网络爬虫1 - 爬取网易LOFTER图片
LOFTER是网易出品的优质轻博客,灵感源于国外的tumblr,但比之更加文艺,更加本地化.本人非常喜欢LOFTER的UI设计,以及其中的优质用户和内容,似乎网易并不擅长推广,所以受众并不广泛.这都是 ...
- python selenium 验证码识别_Python网络爬虫之如何用代码识别图片验证码
验证码 当我们在爬取某些网站的时候,对于一些频繁请求,网站会识别你是机器还是人.如果是机器,直接不允许你访问这个网站了,直接返回404或者禁止访问. 最常见的方式就是验证码.验证码的主要功能就是区分当 ...
- python代码大全p-Python网络爬虫实战项目代码大全(长期更新,欢迎补充)
WechatSogou[1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典.[1]: https://github ...
- python 爬虫论_Python网络爬虫(理论篇)
欢迎关注公众号:Python爬虫数据分析挖掘,回复[开源源码]免费获取更多开源项目源码 网络爬虫的组成 网络爬虫由控制节点,爬虫节点,资源库构成. 网络爬虫的控制节点和爬虫节点的结构关系 控制节点(爬 ...
- Python开发实战案例之网络爬虫(附源码)-张子良-专题视频课程
Python开发实战案例之网络爬虫(附源码)-35人已学习 课程介绍 课程特色: 特色1:案例驱动-围绕两大完整的Python网络爬虫实战开发案例:IT电子书下载网络爬虫和股票交易数据 ...
- 第一章 爬虫(认识网络爬虫)
第一章 认识网络爬虫 1.1 什么是网络爬虫 网络爬虫: 一种按规则,自动请求网站并提取网页数据的程序或脚本 网络爬虫分类(按照系统结构和技术划分): 1.通用网络爬虫 2.聚焦网络爬虫 3 ...
最新文章
- 史上最全中文语料数据集
- 国2c语言中指针与数组的赋值运算,C语言到汇编-指针与数组2
- 数据结构与算法 / 分治算法
- 如何打印CloudFoundry上应用运行时的环境变量
- 前端学习(2862):简单秒杀系统学习之前端优化图片
- OpenShift 4 - 在离线环境中用 oc-mirror 获取 OpenShift 和 Opeartor 的相关镜像
- oracle语句优化积累
- 驱动外置+原版安装方式『XLOS_Windows8_Pro_X86纯净版_V1.0』
- 开源大数据:Databricks Lakehouse
- javascript的getElementByTagName
- 鼠标连点器(VB.net 全局键盘钩子+模拟鼠标 左键、右键)
- 【工控老马】OPC通讯协议解析-OPC七问
- 个人网站性能优化经历(6)网站安全方面优化
- 四色菊皇家大学 SiSaKet Rajabhat University (SSKRU)
- 究竟哪些语句是属于DDL?
- 如何检测新移动硬盘--HD Tune Pro硬盘工具
- 计算机设备驱动的作用,驱动程序是什么意思,电脑驱动程序有什么作用!
- php命令提示无效,window_Win7系统命令提示符输入指令无效问题的解决办法,Win7系统命令提示符输入指令无 - phpStudy...
- 机器学习—特征工程—OneHotEncoder独热编码
- Oracle问题:ORA-01109解决办法