本文实例讲述了python3 BeautifulSoup模块使用字典的方法抓取一个标签内的数据。分享给大家供大家参考,具体如下:
# - * -编码:utf - 8 - * # python 2.7 # XiaoDeng # http://tieba.baidu.com/p/2460150866 #标签操作 从bs4进口BeautifulSoup 进口urllib.request 进口再保险 #如果是网址,可以用这个办法来读取网页 # html_doc=" http://tieba.baidu.com/p/2460150866 " #点播=urllib.request.Request (html_doc) #网页=urllib.request.urlopen(要求) # html=webpage.read () html=" " & lt; html> & lt; head> & lt; title>榛睡鼠的story & lt;/head> & lt; body> & lt; p类="标题" name=" dromouse祝辞& lt; b>榛睡鼠的story & lt;/p> 类& lt; p=肮适隆痹诖忧坝腥鲂〗忝?和他们的名字分别是 & lt; a href=" http://example.com/elsie " rel=巴獠縩ofollow”rel=巴獠縩ofollow”class="妹妹" id=皒iaodeng”祝辞& lt; !——埃尔希祝辞& lt;/a> & lt; a href=" http://example.com/lacie " rel=巴獠縩ofollow”rel=巴獠縩ofollow”class="妹妹" id=發ink2”祝辞Lacie和 & lt; a href=" http://example.com/tillie " rel=巴獠縩ofollow”class="妹妹" id=發ink3”祝辞Tillie; & lt; a href=" http://example.com/lacie " rel=巴獠縩ofollow”rel=巴獠縩ofollow”class="妹妹" id=皒iaodeng”祝辞Lacie 他们住在井底。;/p> & lt; p类="故事"在……& lt;/p> ”“” 汤=BeautifulSoup (html、html.parser) #文档对象 #查找一个标签,只会查找出一个一个标签 #打印(soup.a) # & lt;一个类="妹妹" href=" http://example.com/elsie " rel=巴獠縩ofollow”rel=巴獠縩ofollow”id=皒iaodeng”祝辞& lt; !——埃尔希祝辞& lt;/a> 在soup.find_all k (a): 打印(k) 打印(k[‘类’])#查标签的类属性 打印(k [' id ']) #查标签的id值 打印(k [“href”]) #查标签的href值 打印(k.string) #查标签的字符串 #如果,标签中含有其他标签,比如 . ,此时要提取中的数据,需要用k.get_text () # tag.get(“属性”),也可以达到这个效果 Python套接字编程技巧总结》,《巨蟒正则表达式用法总结》,《Python数据结构与算法教程》、《Python函数使用技巧总结》,《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
更多关于Python相关内容可查看本站专题:《
希望本文所述对大家Python程序设计有所帮助。