python爬虫入门——正则表达式的学习
2017-01-05 17:35
591 查看
1. 正则表达式
RegexPal:在线测试正则表达式网站
抓取网站上的图片的 URL链接,非常直接的做法就是用 findAll("img") 抓取所有图片,但是还有隐藏图片的时候,需要用正则表达式来筛选了。
import urllib2 import bs4 import re html = urllib2.urlopen("http://www.pythonscraping.com/pages/page3.html") bsObj = bs4.BeautifulSoup(html.read(), "lxml") images = bsObj.findAll("img",{"src":re.compile("\.\.\/img\/gifts/img.*\.jpg")}) for image in images: print(image["src"])
这段代码会打印出图片的相对路径,都是以 ../img/gifts/img 开头,以 .jpg 结尾
2. Lambda表达式
Lambda 表达式本质上就是一个函数,可以作为其他函数的变量使用;也就是说,一个函数不是定义成 f(x, y),而是定义成 f(g(x), y),或 f(g(x), h(x)) 的形式。
#获取有两个属性的标签:
soup.findAll(lambda tag: len(tag.attrs) == 2)
结果:
<div class="body" id="content"></div>
<span style="color:red" class="title"></span>
相关文章推荐
- 【python爬虫学习笔记】06 正则表达式以及Re库入门
- python 爬虫入门之正则表达式 一
- [转载]Python爬虫入门七之正则表达式
- python re库的正则表达式入门学习教程
- [置顶] Python爬虫学习(四)正则表达式
- python爬虫入门教程--正则表达式完全指南(五)
- Python爬虫学习纪要(四):正则表达式1
- python3爬虫入门之正则表达式
- Python爬虫入门,如何使用正则表达式
- python3爬虫之入门和正则表达式
- Python爬虫入门七之正则表达式
- 【python爬虫学习2.正则表达式】
- python学习------正则表达式总结(爬虫实例)
- 第01阶段-基础入门-02-Python 爬虫基础-21节=====12.认识正则表达式
- python正则表达式简单爬虫入门+案例(爬取猫眼电影TOP榜)
- Python3爬虫之入门和正则表达式
- python——爬虫学习——正则表达式与Re库-(4)
- 在学习python网络爬虫时用到的正则表达式
- Python爬虫包 BeautifulSoup 学习(九) 正则表达式与Lambda表达式
- python3 爬虫入门(三)正则表达式基本使用