Python网络数据采集笔记第二章
2018-12-02 21:54
232 查看
从find、find_all函数谈起
在此贴出书中对这两个函数的解析
谈谈对html、CSS的个人理解
由于没有涉及过网页编写,单从find_all和find函数输出的网页内容总结出一些规律:(实例:虚拟的在线购物网站 自行打开并用Python输出)
1、< >表示tag标签
是find_all函数中引用的tag项,是仅从html的结构意义上的标题。
2、< >内的等式
表示html内容的属性表示,是find_all函数中引用的attributes项。可用于精准搜索某一特征,如文中金色的字体。attributes项表示的内容也可用keywords项代替(注意python中的保护字,如class,作class_处理)。
children()、decendants()、next_siblings()与parents()
用法见书本P17-P19
正则表达式
正则表达式与BeautifulSoup搭配使用,使得搜索内容更加个性化。正则表达式的特征描述在find_all函数的attributes项。示例代码:
from urllib.request import urlopen from bs4 import BeautifulSoup import re html = urlopen("http://www.pythonscraping.com/pages/page3.html") bsObj = BeautifulSoup(html) images = bsObj.findAll("img",{"src":re.compile("\.\.\/img\/gifts/img.*\.jpg")}) for image in images: print(image["src"])
获取属性
获取tag的属性:BeautifulSoup文件名.tag.attrs[“属性名”],示例代码:
from urllib.request import urlopen from bs4 import BeautifulSoup html=urlopen("http://www.pythonscraping.com/pages/page3.html") bsObj=BeautifulSoup(html) List=bsObj.find_all("img") #获取所有的图片信息 for l in List: print(l.attrs["src"]) #打印出图片信息中"src"的属性
Lambda表达式
除了正则表达式外,算是另一种个性化爬取数据的方式。注意事项由书本给出
补充库
若BeautifulSoup库无法满足需求,可参考lxml库、HTML parser库。
待更
相关文章推荐
- python网络数据采集学习笔记:第二章
- python网络数据采集学习笔记-前言
- 笔记之Python网络数据采集
- 『python学习』python 网络数据采集笔记
- Python网络数据采集学习笔记1
- 笔记之Python网络数据采集
- Python网络数据采集(1)——获取网页源码
- 初识python爬虫 Python网络数据采集1.0 BeautifulSoup通过网站css爬取信息
- python网络数据采集-图像识别与文字处理
- Python网络数据采集6(译者:哈雷)
- Python 网络数据采集——较好的资源
- 好书推荐:Python网络数据采集
- Python网络数据采集——未完待续
- Python网络数据采集
- 初识python爬虫 Python网络数据采集1.0 BeautifulSoup安装测试
- Python网络数据采集4(译者:哈雷)
- Python网络数据采集7(译者:哈雷)
- Python网络爬虫笔记(4)网络采集的常用工具
- python网络数据采集-处理格式规范的文字
- [Python]网络数据采集概述(3)—穿越网页表单、登录窗口进行采集