资源简介
师兄写的python 爬虫的程序,师兄写的python 爬虫的程序
代码片段和文件信息
# -*- coding: cp936 -*-
import urllib
import urllib2
import httplib
import threading
import re
import string
import time
import os
import StringIO
import gzip
from urllib2 import URLErrorHTTPError
from httplib import BadStatusLine
class spider(threading.Thread):
def __init__(selfthreadnameresultstartpagebasekeywordpathmode=0):
threading.Thread.__init__(selfname=threadname)
self.result=result
self.startpage=startpage
self.base=base
self.keyword=keyword
self.path=path
self.mode=mode #爬取的类型:商品 公司
def run(self):
starttime=time.clock()
retry=0 #设置连接次数
page=self.startpage
while 1:
if(page>70): #公司类
endtime=time.clock()
print self.getName()+‘thread finish total time:%d‘%(endtime-starttime)
#print self.result
print ‘**********%d‘%len(self.result)
break
if(page>10 and self.mode==1): #商品类
endtime=time.clock()
print self.getName()+‘thread finish total time:%d‘%(endtime-starttime)
#print self.result
print ‘**********%d‘%len(self.result)
break
try:
url=“http://www.baidu.com/s?wd=“+urllib.quote(self.keyword)+“&pn=“+str(page*10)
#url=“http://www.google.com.hk/search?q=“+urllib.quote(self.keyword)+‘&hl=zh-CN&newwindow=1&safe=strict&biw=1199&bih=654&prmd=ivnscm&ei=HMOCTeWyDo_RcfjR_ZkD&start=‘+str(page*10)
req=urllib2.Request(url)
req.add_header(“User-Agent“‘Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.5) Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5‘)
response=urllib2.urlopen(req)
html=response.read()
self.searchResult(htmlself.mode)
page=page+self.base
except HTTPErrore:
print “the server can‘t fullfill the request.\n“
print “Error code:“e.code
page=page+self.base
except URLErrore:
if(retry>2):
print “can‘t open the %d page url“%page
print “URLError:“e.reason
page=page+self.base
retry=0
else:
print “retry connnect to:%d page“%page
相关资源
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
评论
共有 条评论