PHP前端开发

python用BeautifulSoup抓取div标签的实例教程

百变鹏仔 9小时前 #Python
文章标签 实例教程

这篇文章主要介绍了python 3利用beautifulsoup抓取p标签的方法,文中给出了详细的示例代码供大家参考学习,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。

前言

本文主要介绍的是关于python 3用BeautifulSoup抓取p标签的方法示例,分享出来供大家参考学习,下面来看看详细的介绍:

示例代码:

# -*- coding:utf-8 -*-#python 2.7#XiaoDeng#http://tieba.baidu.com/p/2460150866#标签操作from bs4 import BeautifulSoupimport urllib.requestimport re#如果是网址,可以用这个办法来读取网页#html_doc = "http://tieba.baidu.com/p/2460150866"#req = urllib.request.Request(html_doc) #webpage = urllib.request.urlopen(req) #html = webpage.read()html="""<title>The Dormouse's story</title><p><b>The Dormouse's story</b></p><p>Once upon a time there were three little sisters; and their names were<a><!-- Elsie --></a>,<a>Lacie</a> and<a>Tillie</a>;<a>Lacie</a>and they lived at the bottom of a well.</p><p><img  alt="python用BeautifulSoup抓取div标签的实例教程" >加载中…</p><p>   <span>个人资料</span>   <span>      </span></p><p>           </p>
          

更多>>

     

...

"""soup = BeautifulSoup(html, 'html.parser') #文档对象# 类名为xxx而且文本内容为hahaha的pfor k in soup.find_all('p',class_='atcTit_more'):#,string='更多' print(k) #

更多>>