网络爬虫学习(二)
来源:互联网 发布:linux防火墙禁止ip 编辑:程序博客网 时间:2024/04/29 07:39
网络爬虫学习(二)
1、BeautifulSoup 基础操作
上次代码为:
from bs4 import BeautifulSouphtml_sample=' \<html> \<body> \<h1 id="title"> Hello World</h1> \<a href="#" class="link">This is link1</a> \<a href="# link2" class="link"> This is link2</a> \</body> \</html>'soup=BeautifulSoup(html_sample,'html.parser')print (soup.text)
(1)找出所有含特定标签的HTML元素
- 使用select找出含有h1标记的元素
soup=BeautifulSoup(html_sample,'html.parser')header=soup.select('h1')print (header)
print (header[0])print (header[0].text)
- 使用select找出含有a标签的元素
soup=BeautifulSoup(html_sample,'html.parser')alink=soup.select('a')print (alink)for link in alink:#将标签分2行打印 #print (link) print (link.text)#取出标签里面的内容
(2)取得含有特定CSS属性的元素
- 使用select找出所有id为title的元素(id前面需加#)
alink=soup.select('#title')print (alink)
- 使用select找出所有class为link的元素(class前面需加.)
soup=BeautifulSoup(html_sample)for link in soup.select('.link'):print (link)
(3)取得所有a标签内的链接
- 使用select找出所有a tag的href链接
alinks=soup.select('a')for link in alinks:print (link['href'])
示例
a='<a href="#" qoo=123 abc=456> I am a linker</a>'soup2=BeautifulSoup(a,'html.parser')print (soup2.select('a'))print (soup2.select('a')[0]['abc'])#输出abc后面的值print (soup2.select('a')[0]['qoo'])#输出qoo后面的值
2、观察元素抓取位置
使用检查工具,可以看到新闻内容上面显示的“news_item.first-news-item”
其代码为:
3、根据不同HTML标签取得对应的内容
4、完成所有的爬虫
代码如下:
import requestsfrom bs4 import BeautifulSoupres=requests.get('http://news.sina.com.cn/china/')res.encoding='utf-8'soup= BeautifulSoup(res.text,'html.parser')for news in soup.select('.news-item'): if len(news.select('h2'))>0: h2=(news.select('h2')[0].text) time=(news.select('.time')[0].text) a=news.select('a')[0]['href'] print (time,h2,a)
结果:
0 0
- 网络爬虫学习(二)
- 网络爬虫学习笔记(二)
- 学习python写网络爬虫(二)
- Python学习笔记(二)网络刷博器爬虫
- java网络爬虫学习记录(二)抓包分析
- 学习笔记—Python网络爬虫(二)
- 爬虫学习(二)
- 网络爬虫基本原理(二)
- 网络爬虫基本原理(二)
- 网络爬虫基础(二)
- python网络爬虫(二)
- 网络爬虫规则(二)
- python网络爬虫(二)
- Python网络爬虫学习scrapy(二)
- 网络爬虫学习(1)
- 网络爬虫学习(一)
- 网络爬虫学习(三)
- 爬虫学习记录(二)
- 1002:写出这个数
- 【weex】第一次尝试,weex windows下的环境搭建到第一个完整的app
- ubuntu 安装 jdk 1.8
- 凸包算法
- 中介者模式 1
- 网络爬虫学习(二)
- 剑指offer_23
- QT串口程序已16进制接受和发送数据
- 职场面对勾心斗角该怎么办
- CentOS7安装mysql数据库
- JAVA——final关键字
- MySQL索引字段大小
- C++关键字之const
- 浅谈HTML5 canvas(一)