import  requests
res = requests.get(newsUrl)
res.encoding ='utf-8’
pint
print(res.text)
//然后通过DOM Tree来剖析网页元素
 
 
 
from bs4 import BeautifulSoup
html_sample ='\
<html>\
<body>\
<h1 id="title">this is h1</h1>\
<a class="link" href="fdfdfdfd">this is a link</a>\
<a class="link" href="fdfdfdfd">this is another link</a>\
</body>\
</html>'
'''
html.parser  解析器 ,不写的话会发出警告
'''
soup = BeautifulSoup(html_sample,'html.parser’)
print(soup.text)
#找出所有含特定标签的HTML元素
 
#1: 使用select 找出含有h1标签的元素
header = soup.select('h1’)
print(header)print(header[0].text ) 
#第0个标签中的文字
 
#2: 使用select找出含有a标签的元素
alink = soup.select('a’)
print(alink)
for link in alink:
#print(link)
print(link.text)
#取得含有特定CSS属性的元素
#1使用select找出所有id为title的元素(id前需加#)
 
aTitle = soup.select('#title')
print(aTitle)
#2使用select找出所有class为link的元素(class前需要加.)
for mylink in soup.select('.link'):
print(mylink)
#取得所有a标签内的链接
#使用select找出所有a tag的href连结
 
ahref = soup.select('a')
for ah in ahref:
   
print(ah['href']) 

最新文章

  1. C 标准库系列之概述
  2. TTS通过JavaScript调用
  3. (一)u-boot-nand.bin的下载
  4. python数据结构-列表-基本操作
  5. JMeter 测试Web登录
  6. hdu 1242 Rescue_bfs+优先队列
  7. firefox必备扩展
  8. php实现多表(四表)连接
  9. .NET日志工具介绍
  10. find用法积累
  11. SQL生成一年每一天的时间列表的几种方法
  12. Docker 入门之创建service(一)
  13. Hyperledger Fabric-CA学习
  14. Document.write和 getElementById(ID)
  15. Swift中 @objc 使用介绍
  16. Python元组与列表的区别
  17. linux工具介绍
  18. toFixed四舍五入精度校正
  19. Android Studio 入门级教程(一)
  20. python爬虫之Scrapy框架(CrawlSpider)

热门文章

  1. 使用swoole编写简单的echo服务器
  2. 转 移动端-webkit-user-select:none导致input/textarea输入框无法输入
  3. .netcore webapi iis 虚拟目录下载apk文件
  4. u-boot之ARM920T的start.S分析
  5. glide install失败 Update failed for golang.org/x/net: Cannot detect VCS
  6. 安卓个性化 Button
  7. Startup.国外新锐公司及其技术Blog
  8. Luogu1501 Tree II - LCT
  9. tableView的cell之间间隔显示空白区域
  10. Servlet开发的三种方法