python3用BeautifulSoup用字典的方法抓取a标签内的数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python3用BeautifulSoup用字典的方法抓取a标签内的数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1511字,纯文字阅读大概需要3分钟。
内容图文
# -*- coding:utf-8 -*- # python 2.7 # XiaoDeng # http://tieba.baidu.com/p/2460150866 # 标签操作 from bs4 import BeautifulSoup import urllib.request import re #如果是网址,可以用这个办法来读取网页 #html_doc = "http://tieba.baidu.com/p/2460150866" #req = urllib.request.Request(html_doc) #webpage = urllib.request.urlopen(req) #html = webpage.read() html=""" <html><head><title>The Dormouse‘s story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse‘s story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="xiaodeng"><!-- Elsie --></a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; <a href="http://example.com/lacie" class="sister" id="xiaodeng">Lacie</a> and they lived at the bottom of a well.</p> <p class="story">...</p> """ soup = BeautifulSoup(html, ‘html.parser‘) #文档对象#查找a标签,只会查找出一个a标签 #print(soup.a)#<a class="sister" href="http://example.com/elsie" id="xiaodeng"><!-- Elsie --></a>for k in soup.find_all(‘a‘): print(k) print(k[‘class‘])#查a标签的class属性print(k[‘id‘])#查a标签的id值print(k[‘href‘])#查a标签的href值print(k.string)#查a标签的string#tag.get(‘calss‘),也可以达到这个效果
原文:http://www.cnblogs.com/dengyg200891/p/6060010.html
内容总结
以上是互联网集市为您收集整理的python3用BeautifulSoup用字典的方法抓取a标签内的数据全部内容,希望文章能够帮你解决python3用BeautifulSoup用字典的方法抓取a标签内的数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】