BeautifulSoup4小爬虫
来源:互联网 发布:车主采集软件 编辑:程序博客网 时间:2024/05/29 17:50
BeautifulSoup实例1:
from bs4 import BeautifulSoup html_sample='\ <html>\ <body>\ <h1 id="title">Hello world</h1>\ <a href="" class="link">This is link1</a>\ <a href="" class="link">This is link2</a>\ </body>\ </html>' soup=BeautifulSoup(html_sample,'html.parser')print(soup.text) # 去除标签输出soup的内容header=soup.select('h1') # 搜索soup里的h1标签,再给headerprint(header) print(header[0]) # 把搜索出来的h1标签的第一个打印出来print(header[0].text) #第一个h1标签的内容alink=soup.select('a') # 搜索soup里的a标签,再给alinkprint(alink) print(alink[0]) #把搜索出来的a标签的第一个打印出来print(alink[0].text) #第一个a标签的内容for link in alink: #遍历alink print(link)for link in alink: print(link.text)for link in alink: print(link['href']) #遍历a标签里的href属性 a='<a href="#link3" class="link" style="css code">This is link3</a>'soup2=BeautifulSoup(a,'html.parser')print(soup2.select('a')[0]['style']) #打印第一个a标签的style属性
BeautifulSoup实例2:
前面的时间和后面的新闻标题url分别来自2个不同的列表,如何让他们并行输出呢?
可以用zip()并行迭代的方法:
for times,news in zip(time, new)
zip()说明:
例如,有两个列表
a = [1,2,3]b = [4,5,6]
使用zip()函数来可以把列表合并,并创建一个元组对的列表。
zip(a,b)[(1, 4), (2, 5), (3, 6)]
在python 3.0中zip()是可迭代对象,使用时必须将其包含在一个list中,方便一次性显示出所有结果
list(zip(a,b))[(1, 4), (2, 5), (3, 6)]
zip()参数可以接受任何类型的序列,同时也可以有两个以上的参数;当传入参数的长度不同时,zip能自动以最短序列长度为准进行截取,获得元组。
>>> l1,l2,l3 = (1,2,3),(4,5,6),(7,8,9)>>> zip(l1,l2,l3)[(1, 4, 7), (2, 5, 8), (3, 6, 9)]>>> str1 = 'abc'>>> str2 = 'def123'>>> zip(str1,str2)[('a', 'd'), ('b', 'e'), ('c', 'f')]
zip()方法用在for循环中,就会支持并行迭代:
l1 = [2,3,4]l2 = [4,5,6]for (x,y) in zip(l1,l2):print x,y,'--',x*y2 4 -- 83 5 -- 154 6 -- 24
0 0
- BeautifulSoup4小爬虫
- requests+beautifulsoup4 爬虫实战
- 爬虫05 BeautifulSoup4初体验
- python爬虫 BeautifulSoup4官方文档
- 【python小练】图片爬虫之BeautifulSoup4--转自博客园
- 爬虫——使用BeautifulSoup4的爬虫
- Python爬虫辅助库BeautifulSoup4用法精要
- python爬虫——beautifulsoup4使用学习
- Python爬虫之正则 & BeautifulSoup4解析HTML
- Python爬虫BeautifulSoup4系列之十
- BeautifulSoup4
- BeautifulSoup4
- Python3 爬虫(七) -- 配置BeautifulSoup4+lxml+html5lib
- Python进阶(十九)-Python3安装第三方爬虫库BeautifulSoup4
- 第三课 Python爬虫Beautifulsoup4模块的使用
- python爬虫提取数据之Beautifulsoup4简单使用
- python爬虫提取数据之Beautifulsoup4简单使用
- python简单爬虫,Beautifulsoup4解析,爬取直播吧部分热门新闻及链接
- HDOJ 1710Binary Tree Traversals 二叉树的遍历
- LeetCode - 20. Valid Parentheses
- div中img有空白的处理
- 使用UMDH进行内心泄露分析
- POJ 1635 Subway tree systems
- BeautifulSoup4小爬虫
- html标签 后台转义
- 软件测试面试题集合
- 发布到tomcat服务器上webapps里的文件名和项目名称不一样
- 编译安装mysql
- URAL 1486 Equal Squares
- RSA算法原理及其在HTTPS中的应用
- Xcode快捷键及代码块
- 文本替换与删除