第一个python爬虫(python3爬取百度百科1000个页面)
来源:互联网 发布:ip网络广播控制中心 编辑:程序博客网 时间:2024/05/22 09:22
以下内容参考自:http://www.imooc.com/learn/563
一、爬虫简介
爬虫:一段自动抓取互联网信息的程序
爬虫可以从一个url出发,访问其所关联的所有的url。并从每个url指向的网页中,获取我们所需要的信息。
二、简单爬虫架构
1.Python简单爬虫架构
(1)爬虫调度端:启动爬虫、停止爬虫、监视爬虫的运行情况。
(2)在爬虫程序中,有三个模块:
1)Url管理器:管理将要爬取的url和已经爬取的url。将待爬取的url传送给网页下载器。2)网页下载器:将Url指定的网页下载下来,保存为一个字符串。将这个字符串传送给网页解析器进行解析。3)网页解析器:一方面,会解释出有价值的数据;另一方面,解析出字符串中的url,将其补充到url管理器。这三个模块,形成了一个循环。只有有未爬取的url,这个循环就会一直继续下去。
2.Python简单爬虫架构的动态运行流程
三、分别讲解各模块设计思路
1.URL管理器
作用:管理待抓取URL集合和已抓取URL集合
目的:防止重复抓取、防止循环抓取
实现方式:
在这里,我们选用Python的set()来实现小型的url管理器
2.网页下载器
作用:将互联网上URL对应的网页下载到本地的工具
工作流程:
实现方式:
3.网页解析器
作用:从网页中提取有价值的数据
从下载好的html网页或者字符串中,可以提取出url、有价值的数据。
类型:正则表达式、html.parser、BeautifulSoup、lxml
其中,BeautifulSoup这个第三方插件可以使用html.parser和lxml作为它的解析器。
其中,正则表达式是模糊匹配,另外三种则是结构化的解析。
附,结构化解析:
spider_main.py四、实战:使用Python爬虫抓取百度百科的1000个页面
1.分析目标
目标:百度百科Python词条相关词条网页-标题和简介
入口页:http://baike.baidu.com/view/21087.htm
URL格式:
--词条页面URL:/view/21087.htm
数据格式:
--标题:<dd class="lemmaWgt-lemmaTitle-title"><h1>***</h1></dd>
--简介:<div class="lemma-summary" label-module="lemmaSummary">***</div>
页面编码:UTF-8
2.调度程序
程序目录:
from baike_spider import html_downloaderfrom baike_spider import html_parserfrom baike_spider import html_outputerclass 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(): try: new_url = self.urls.get_new_url() print("craw %d : %s" %(count, new_url)) html_cont = self.downloader.download(new_url) new_urls, new_data = self.parser.paser(new_url, html_cont) self.urls.add_new_urls(new_urls) self.outputer.collect_data(new_data) if count == 1000: break count = count + 1 except: print('craw failed') self.outputer.output_html()if __name__ == '__main__': root_url = "https://baike.baidu.com/item/Python" obj_spider = SpiderMain() obj_spider.craw(root_url)html_download.pyimport urllib.requestclass HtmlDownloader(object): def download(self, url): if url is None: return None response = urllib.request.urlopen(url) if response.getcode() != 200: return None return response.read()url_manager.pyclass 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_urlhtml_parser.pyfrom bs4 import BeautifulSoupimport reimport urllib.parseclass HtmlParser(object): def _get_new_urls(self, page_url, soup): new_urls = set() links = soup.find_all('a', href=re.compile(r'/item/')) for link in links: new_url = link['href'] new_full_url = urllib.parse.urljoin(page_url, new_url) new_urls.add(new_full_url) return new_urls def _get_new_data(self, page_url, soup): res_data = {} # url res_data['url'] = page_url title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find('h1') res_data['title'] = title_node.get_text() # lemma-summary summary_node = soup.find('div', class_="lemma-summary") res_data['summary'] = summary_node.get_text() return res_data def paser(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_datahtml_outputer.pyclass 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', encoding="utf-8") fout.write("<html>") fout.write("<head><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"></head>") 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']) fout.write("<td>%s</td>" % data['summary']) fout.write("</tr>") fout.write("</table>") fout.write("</body>") fout.write("</html>") fout.close()
运行结果如下
还会生成一个html记录文件
项目github地址:https://github.com/oldbig-carry/python_baidu_spider
阅读全文
1 0
- 第一个python爬虫(python3爬取百度百科1000个页面)
- 简单爬虫-爬取百度百科1000个页面
- 【Python爬虫】爬取百度百科python相关的1000个页面
- 实践项目十:爬取百度百科Python词条相关1000个页面数据(慕课简单爬虫实战)
- Python爬取百度百科1000个页面
- 我的第一个Python3 网络爬虫 百度百科爬虫
- python3爬虫(1)--百度百科的页面爬取
- Python爬虫----实例: 抓取百度百科Python词条相关1000个页面数据
- 第一个Python3.0 爬虫程序, 爬取百度贴吧图片
- 编写Python代码——爬取百度百科Python词条相关1000个页面数据【未完慕课】
- Python3爬虫之四简单爬虫架构【爬取百度百科python词条网页】
- Python爬虫_BeautifulSoup爬取百度百科
- Python爬虫,爬取百度百科词条
- Python爬虫爬取百度百科词条
- 简单的python爬虫(爬取百度百科词条)
- Python 简单爬虫实现(爬取百度百科信息)
- python3 爬虫学习-根据关键词爬取百度百科内容
- python3.6.2实现的简单爬虫爬取百度百科
- shell概述
- 【C语言】【unix c】为类型命名别名(使用typedef关键字)
- 27. Remove Element
- pyqt笔记
- 从零开始学Scala(一)——Scala环境搭建与第一行代码
- 第一个python爬虫(python3爬取百度百科1000个页面)
- 网络编程学习之——TCP通信协议
- pthread_once()
- poj2299 B
- NSF服务 -- 文件共享 (windows挂载centos)
- poj2559 Largest Rectangle in a Histogram (单调栈)
- Java设计模式实战-Builder
- MyBatis实现简单的用户查询
- 散乱知识点1