html_parser.py
coding=UTF-8
# HTML解释器
import re
from bs4 import BeautifulSoup
class htmlParser():
def parse(self, url, html_cont):
if 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(url, soup)
new_data = self._get_new_datas(url, soup)
return new_urls, new_data
def _get_new_urls(self, url, soup):
urls = set()
# <a target="_blank" href="/item/%E8%A7%A3%E9%87%8A%E5%99%A8">解释器</a>
links = soup.find_all('a', href=re.compile(r'/item/\S+'))
for link in links:
new_url = link['href']
new_full_url = 'http://baike.baidu.com' + new_url
urls.add(new_full_url)
return urls
def _get_new_datas(self, url, soup):
rst_data = {}
# url
rst_data['url'] = url
# <dd class="lemmaWgt-lemmaTitle-title"> <h1>Python</h1>
title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find('h1') # class为关键字,需要后面加下划线
rst_data['title'] = title_node.get_text()
# <div class="lemma-summary">
summary_node = soup.find('div', class_="lemma-summary")
rst_data['summary'] = summary_node.get_text()
return rst_data
html_parser.py的更多相关文章
- 爬虫3 html解析器 html_parser.py
#coding:utf8 import urlparse from bs4 import BeautifulSoup import re __author__ = 'wang' class HtmlP ...
- python爬虫—爬取百度百科数据
爬虫框架:开发平台 centos6.7 根据慕课网爬虫教程编写代码 片区百度百科url,标题,内容 分为4个模块:html_downloader.py 下载器 html_outputer.py 爬取数 ...
- Python抓取百度百科数据
前言 本文整理自慕课网<Python开发简单爬虫>,将会记录爬取百度百科"python"词条相关页面的整个过程. 抓取策略 确定目标:确定抓取哪个网站的哪些页面的哪部分 ...
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...
- Python 入门级报错处理
问题1:Missing parentheses in call to 'print' 原因:因为Python2.X和Python3.X不兼容.我安装的是Python3.X,但是我试图运行的却是Pyth ...
- 自己动手python打造渗透工具集
难易程度:★★★阅读点:python;web安全;文章作者:xiaoye文章来源:i春秋关键字:网络渗透技术 前言python是门简单易学的语言,强大的第三方库让我们在编程中事半功倍,今天我们就来谈谈 ...
- Python开发简单爬虫
简单爬虫框架: 爬虫调度器 -> URL管理器 -> 网页下载器(urllib2) -> 网页解析器(BeautifulSoup) -> 价值数据 Demo1: # codin ...
- python打造渗透工具集
python是门简单易学的语言,强大的第三方库让我们在编程中事半功倍,今天我们就来谈谈python在渗透测试中的应用,让我们自己动手打造自己的渗透工具集. 难易程度:★★★阅读点:python;web ...
- python爬虫:爬取慕课网视频
前段时间安装了一个慕课网app,发现不用注册就可以在线看其中的视频,就有了想爬取其中的视频,用来在电脑上学习.决定花两天时间用学了一段时间的python做一做.(我的新书<Python爬虫开发与 ...
随机推荐
- canvas制作雪花效果
<!DOCTYPE html><html> <head> <meta http-equiv="Content-type" conte ...
- appium-python-api中文文档
来自https://wenku.baidu.com/view/533603ce581b6bd97e19eaa1.html mark,同时提供给需要使用python写脚本的童鞋们
- 浏览器-http协议简介
HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...
- PHP 简单调用rest WebServices
<?php $ch = curl_init("http://api.cachk.com:8185/Tech-Trans.esPOS4.PosService.UAT/rest/enqui ...
- C++ new new[]详解
精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...
- CentOS系统中使用iptables设置端口转发
echo 1 > /proc/sys/net/ipv4/ip_forward 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0 ...
- object dection资源
https://handong1587.github.io/deep_learning/2015/10/09/object-detection.html
- keyframes 放大缩小动画
本次项目中动画放大缩小代码小结 .fix .phone{ -moz-animation: myfirst 1s infinite; -webkit-animation: myfirst 1s infi ...
- Swiper插件
中文官网:Swiper中文网 英文:英文网 此插件功能比较强大,网页端.手机端都可以使用的插件.这里记录一下在微信h5页面里面滑动获取数据. 先下载css和js,引用到项目中 这里有6个节点,没划一个 ...
- C#操作Word,写数据,插入图片
本篇介绍的是如何在C#中往word里面写入数据. 如何在线的操作文档: c#在线操作文档 关于Aspose.Word控件的介绍,请戳→ 介绍 首先需要去下载这个dll文件,然后引用到你的项目当中.地 ...