需求:抓取百度百科python词条相关词条网页的标题和简介,并将数据输出在一个html表格中

入口页:python的百度词条页 https://baike.baidu.com/item/Python/407313

词条页面URL:'/item/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E8%AF%AD%E8%A8%80' 注意:这不是一个完整的url,需要对之进行拼接

数据格式:

 -标题:<dd class="lemmaWgt-lemmaTitle-title"><h1>***</h1></dd>
-简介:<div class='lemma-summary'>***</div>
页面编码:UTF-8 实例代码:
文件目录结构如图



入口文件(spider_main.py):
# coding:utf-8
import url_manager
import html_parser
import html_downloader
import html_outputer class SpiderMain(object):
def __init__(self):
self.urls = url_manager.UrlManager()
self.downloader = html_downloader.HtmlDownloader()
self.parser = html_parser.HtmlParser()
self.outputer = html_outputer.HtmlOutputer() def craw(self, root_url):
count = 1
self.urls.add_new_url(root_url)
while self.urls.has_new_url(): # 如果有待爬取的url
try:
new_url = self.urls.get_new_url() # 取一个待爬取的url
print 'craw %d:%s' % (count, new_url)
html_cont = self.downloader.download(new_url) # 下载页面
new_urls, new_data = self.parser.parse(new_url, html_cont) # 解析页面得到新的url和一些数据
self.urls.add_new_urls(new_urls) # 将新得到的url添加到url管理器
self.outputer.collect_data(new_data) # 将获取到的数据添加到output文件中
if count == 10:
break
count += 1
except Exception as e:
print e
self.outputer.output_html() if __name__ == '__main__':
obj_spider = SpiderMain()
obj_spider.craw('https://baike.baidu.com/item/Python/407313')

url管理文件(url_manager.py):

# coding:utf-8

class UrlManager(object):
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 add_new_urls(self, urls):
if urls is None or len(urls) == 0:
return
for url in urls:
self.add_new_url(url) def has_new_url(self):
return len(self.new_urls) != 0 def get_new_url(self):
new_url = self.new_urls.pop() # 从列表中获取一个并且移除
self.old_urls.add(new_url)
return new_url

页面源码下载(html_downloader.py):

# coding:utf-8
import urllib2 class HtmlDownloader(object):
def download(self, url):
if url is None:
return None
response = urllib2.urlopen(url)
if response.getcode() != 200:
return None
return response.read()
源码解析(html_parser.py):
# coding:utf-8
from bs4 import BeautifulSoup
import urlparse
import re class HtmlParser(object):
def _get_new_urls(self, page_url, soup):
new_urls = set()
# 页面中的链接格式:/item/xxx
links = soup.find_all('a', href=re.compile(r"/item/(.*)")) # 得到所有的url的标签
for link in links:
new_url = link['href'] # 获取所有的链接
new_full_url = urlparse.urljoin(page_url, new_url) # 让new_url按照page_url的格式拼接成一个完整的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
# <dd class="lemmaWgt-lemmaTitle-title"><h1>Python</h1></dd>
title_node = soup.find('dd', class_='lemmaWgt-lemmaTitle-title').find('h1')
res_data['title'] = title_node.get_text()
# <div class="lemma-summary" label-module="lemmaSummary"> 简介的html
summary_node = soup.find('div', class_='lemma-summary')
res_data['summary'] = summary_node.get_text()
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)
return new_urls, new_data

将爬取到的数据输出(html_output.py):

# coding:utf-8

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): # 将数据输出为html
fout = open('output.html', 'w')
fout.write('<html>')
fout.write('<body>')
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>')

用python编写简单爬虫的更多相关文章

  1. Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...

  2. Python 利用Python编写简单网络爬虫实例2

    利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...

  3. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

  4. 使用Python编写简单的端口扫描器的实例分享【转】

    转自 使用Python编写简单的端口扫描器的实例分享_python_脚本之家 http://www.jb51.net/article/76630.htm -*- coding:utf8 -*- #!/ ...

  5. python编写简单的html登陆页面(4)

    python编写简单的html登陆页面(4)   1  在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将动态态分配数据,建立表格,存放学生信息 2 实现的效果如下: 3  动 ...

  6. python编写简单的html登陆页面(3)

    1  在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将静态分配数据,建立表格,存放学生信息 2  加载到静态数据 3  html的编写直接在表格里添加一组数据就行了 4  V ...

  7. python编写简单的html登陆页面(2)

    1  在python编写简单的html登陆页面(1)的基础上在延伸一下: 可以将动态分配数据,实现页面跳转功能: 2  跳转到新的页面:return render_template('home1.ht ...

  8. 用Python编写简单的发红包程序和计算器原理

    用Python编写简单的发红包程序: 第一种解法:数轴方法解决 import random def red_packet(money,num): money = money * 100 #将钱数转换成 ...

  9. 使用Python编写简单网络爬虫抓取视频下载资源

    我第一次接触爬虫这东西是在今年的5月份,当时写了一个博客搜索引擎.所用到的爬虫也挺智能的,起码比电影来了这个站用到的爬虫水平高多了! 回到用Python写爬虫的话题. Python一直是我主要使用的脚 ...

随机推荐

  1. 谷歌浏览器安装vuejs devtools 插件

    1 . 下载chrome扩展插件. 在github上下载压缩包并解压到本地,github下载地址:https://github.com/vuejs/vue-devtools 2. npm instal ...

  2. spring3: AOP 之代理机制

    Spring AOP通过代理模式实现,目前支持两种代理:JDK动态代理.CGLIB代理来创建AOP代理,Spring建议优先使用JDK动态代理. JDK动态代理:使用java.lang.reflect ...

  3. yii2: oracle汉字占用字节长度

    OCIStmtExecute: ORA-12899: value too large for column "WSG"."WX_ENTER_TASTE".&qu ...

  4. 打造万能的Python开发环境

    一.下载与安装 python的版本众多,主流的分2和3,还有各种小版本. django等也有众多版本. 在我们开发过程中,为使用不同的版本测试,在一台电脑上进行只能进行替换. 为了简化.我们使用con ...

  5. 软工作业-Wc

    Wc.exe wc.exe是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数.单词数和行 ...

  6. New Concept English three (32)

    26w/m 68 The salvage operation had been a complete failure. The small ship, Elkor, which had been se ...

  7. Mysql中文汉字转拼音的实现(每个汉字转换全拼)

    -- 创建汉字拼音对照临时表 CREATE TABLE IF NOT EXISTS `t_base_pinyin` ( `pin_yin_` varchar(255) CHARACTER SET gb ...

  8. [置顶] C语言学习入门

    编译文件:cc -c one.c two.c  生成.o目标文件 链接文件:cc one.o two.o     默认生成 a.out 执行文件 指定生成的可执行文件名   cc -o one one ...

  9. ubuntu 上的ruby安装

    安装 rbenv git clone git://github.com/sstephenson/rbenv.git ~/.rbenv # 用来编译安装 ruby git clone git://git ...

  10. (四)canvas绘制路径

    save() 样式不受污染的起始范围 shadowOffsetX 阴影x轴的距离 shadowOffsetY 阴影y轴的距离 shadowBlur 模糊度 shadowColor 阴影颜色 resto ...