python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例


Posted in Python onNovember 28, 2019

本文实例讲述了python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据。分享给大家供大家参考,具体如下:

# -*- 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" rel="external nofollow" rel="external nofollow" class="sister" id="xiaodeng"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>;
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" 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" rel="external nofollow" rel="external nofollow" 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
#如果,标签中含有其他标签,比如..,此时要提取中的数据,需要用k.get_text()
#tag.get('calss'),也可以达到这个效果

Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

更多关于Python相关内容可查看本站专题:《

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python学习笔记_数据排序方法
May 22 Python
详细探究Python中的字典容器
Apr 14 Python
python使用线程封装的一个简单定时器类实例
May 16 Python
Python使用Phantomjs截屏网页的方法
May 17 Python
详谈Pandas中iloc和loc以及ix的区别
Jun 08 Python
Python实现的爬取小说爬虫功能示例
Mar 30 Python
pyqt5之将textBrowser的内容写入txt文档的方法
Jun 21 Python
python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)
Aug 28 Python
Django中使用haystack+whoosh实现搜索功能
Oct 08 Python
python getopt模块使用实例解析
Dec 18 Python
Python用来做Web开发的优势有哪些
Aug 05 Python
matplotlib交互式数据光标mpldatacursor的实现
Feb 03 Python
解决Python二维数组赋值问题
Nov 28 #Python
python之array赋值技巧分享
Nov 28 #Python
python3 实现函数写文件路径的正确方法
Nov 27 #Python
关于Python3 lambda函数的深入浅出
Nov 27 #Python
python3中rank函数的用法
Nov 27 #Python
Python values()与itervalues()的用法详解
Nov 27 #Python
python3 assert 断言的使用详解 (区别于python2)
Nov 27 #Python
You might like
php短域名转换为实际域名函数
2011/01/17 PHP
防止用户利用PHP代码DOS造成用光网络带宽
2011/03/01 PHP
Erlang的运算符(比较运算符,数值运算符,移位运算符,逻辑运算符)
2012/07/23 PHP
php提示Warning:mysql_fetch_array() expects的解决方法
2014/12/16 PHP
php格式化金额函数分享
2015/02/02 PHP
javascript下4个跨浏览器必备的函数
2010/03/07 Javascript
利用javascript数组长度循环数组内所有元素
2013/12/27 Javascript
js replace替换所有匹配的字符串
2014/02/13 Javascript
基于jQuery实现的幻灯图片切换
2016/12/02 Javascript
微信小程序 详解页面跳转与返回并回传数据
2017/02/13 Javascript
详解ES6之用let声明变量以及let loop机制
2017/07/15 Javascript
Angular4学习笔记之准备和环境搭建项目
2017/08/01 Javascript
vue中appear的用法
2017/08/17 Javascript
async/await地狱该如何避免详解
2018/05/10 Javascript
jQuery实现侧边栏隐藏与显示的方法详解
2018/12/22 jQuery
layer.open组件获取弹出层页面变量、函数的实例
2019/09/25 Javascript
详解vue中$nextTick和$forceUpdate的用法
2019/12/11 Javascript
[01:23:35]Ti4主赛事胜者组 DK vs EG 1
2014/07/19 DOTA
[01:20]2018DOTA2亚洲邀请赛总决赛战队Mineski晋级之路
2018/04/07 DOTA
Python模仿POST提交HTTP数据及使用Cookie值的方法
2014/11/10 Python
Python3实现从指定路径查找文件的方法
2015/05/22 Python
Python cookbook(数据结构与算法)让字典保持有序的方法
2018/02/18 Python
Flask之请求钩子的实现
2018/12/23 Python
css3 给背景设置渐变色的方法
2019/09/12 HTML / CSS
俄罗斯美容和健康网上商店:Созвездие Красоты
2019/07/23 全球购物
会计专业大学生求职信范文
2014/01/28 职场文书
给国外客户的邀请函
2014/01/30 职场文书
机械设计毕业生自荐信
2014/02/02 职场文书
体育教学随笔感言
2014/02/24 职场文书
出纳员岗位职责风险
2014/03/06 职场文书
求职信模板
2014/05/23 职场文书
规范化管理年活动总结
2014/08/29 职场文书
最美家庭活动方案
2014/08/31 职场文书
SQL Server 数据库实验课第五周——常用查询条件
2021/04/05 SQL Server
关于JavaScript回调函数的深入理解
2021/06/27 Javascript
python基础之错误和异常处理
2021/10/24 Python