首页 技术 正文
技术 2022年11月13日
0 收藏 862 点赞 5,089 浏览 3324 个字

爬虫框架:开发平台 centos6.7 根据慕课网爬虫教程编写代码 片区百度百科url,标题,内容

分为4个模块:html_downloader.py 下载器html_outputer.py 爬取数据生成html模块html_parser 获取有用数据url_manager url管理器spider_main 爬虫启动代码 spider_main.py

 #!/usr/bin/python
#-*- coding: utf8 -*- import html_downloader
import html_outputer
import html_parser
import url_manager class SpiderMain(object):
def __init__(self):
#初始化url管理器
self.urls = url_manager.UrlManager()
#初始化url下载器
self.downloader = html_downloader.HtmlDownloader()
#初始化url解析器
self.parser = html_parser.HtmlParser()
#初始化url输出
self.outputer = html_outputer.HtmlOutputer() def craw(self, root_url):
count = 1
#url管理器中添加一个new url
self.urls.add_new_url(root_url)
#判断是否有新的URL 开始爬取
while self.urls.has_new_url():
try:
#得到新的url
new_url = self.urls.get_new_url()
print 'craw %d : %s' % (count, new_url)
#下载新的url的数据
html_cont = self.downloader.download(new_url)
#解析出来url的内容和地址
new_urls, new_data = self.parser.parse(new_url, html_cont)
#新的url补充进url管理器
self.urls.add_new_urls(new_urls)
#输出数据
self.outputer.collect_data(new_data) if count == 1001:
break
count = count + 1
print count
except:

html_parser.py

 #!/bin/usr/python
#-*- coding:utf8 -*- from bs4 import BeautifulSoup
import re
import urlparse class HtmlParser(object):
'''
解析器
''' def _get_new_urls(self, page_url, soup):
new_urls = set()
links = soup.find_all('a', href=re.compile(r"/view/\d+\.htm"))
for link in links:
new_url = link['href']
new_full_url = urlparse.urljoin(page_url, new_url)
new_urls.add(new_full_url) return new_urls def _get_new_data(self, page_url, soup):
res_data = {} res_data['url'] = page_url title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find('h1')
res_data['title'] = title_node.get_text() summary_node = soup.find('div', class_="lemma-summary")
res_data['summary'] = summary_node.get_text()
print res_data['summary'] return res_data def parse(self, page_url, html_cont):
if page_url is None or html_cont is None:
return soup = BeautifulSoup(html_cont, 'html.parser', from_encoding='utf-8')
new_urls = self._get_new_urls(page_url, soup)
new_data = self._get_new_data(page_url, soup)
print new_data
return new_urls, new_data

html_outputer.py

 #!/usr/bin/python
#-*- coding:utf8 -*- class HtmlOutputer(object):
def __init__(self):
self.datas = [] def collect_data(self, data):
if data is None:
return
self.datas.append(data) def output_html(self):
fout = open('output.html', 'w') fout.write("<html>")
fout.write("<body>")
fout.write("<head>")
fout.write('<meta charset="utf-8">')
fout.write("</head>")
fout.write("<table>") for data in self.datas:
fout.write("<tr>")
fout.write("<td>%s</td>" % data['url'])
fout.write("<td>%s</td>" % data['title'].encode('utf-8'))
fout.write("<td>%s</td>" % data['summary'].encode('utf-8'))
fout.write("</tr>") fout.write("</table>")
fout.write("</body>")
fout.write("</html>") fout.close()

html_downloader.py

 #!/usr/bin/python
#-*- coding:utf8 -*- import urllib2 class HtmlDownloader(object):
'''
下载器
''' def download(self, url):
if url is None:
return None response = urllib2.urlopen(url) if response.getcode() != 200:
print '请求失败'
return None return response.read()

url_manager.py

 #!/usr/bin/python
#-*- coding:utf8 -*- class UrlManager(object):
'''
url管理器
'''
def __init__(self):
self.new_urls = set()
self.old_urls = set() def add_new_url(self, url):
if url is None:
return
if url not in self.new_urls and url not in self.old_urls:
self.new_urls.add(url) def has_new_url(self):
if len(self.new_urls) != 0:
return len(self.new_urls)
else:
print '没有新的url' def get_new_url(self):
new_url = self.new_urls.pop()
self.old_urls.add(new_url)
return new_url def add_new_urls(self, urls):
if urls is None or len(urls) == 0:
return
for url in urls:
self.add_new_url(url)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777