网络爬虫学习(二)

网络爬虫学习(二)


1、BeautifulSoup 基础操作

上次代码为:

from bs4 import BeautifulSoup
html_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 requests
from bs4 import BeautifulSoup
res=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)

结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值