python BeautifulSoup4--例子


from bs4 import BeautifulSoup
import requests
import re

#请求博客园首页
r=requests.get('http://www.cnblogs.com/tangqiu/')

#使用html.parser解析html
soup=BeautifulSoup(r.content,'html.parser')
print(soup.name) #soup.name 为[document]

#使用正则表达式找出所有以t开头的标签,返回一个列表
t=soup.find_all(re.compile('^t'))

#从t列表中找到title,使用.string 获取html的标题
for title in t:
    print(title.string)

#找出所有class="dayTitle"的标签,返回一个列表
tags=soup.find_all(class_="dayTitle")

#打印首页博客的日期
for time in tags:
    print(time.a.string)

#打印首页博客的摘要
abstract=soup.find_all(class_="c_b_p_desc")
for abstract in abstracts:
    print(abstract.contents[0])

 中文官方文档http://beautifulsoup.readthedocs.io/zh_CN/latest/


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM