python 如何获取页面所有a标签下href的值


Posted in Python onMay 06, 2021

看代码吧~

# -*- coding:utf-8 -*-
#python 2.7
#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

如果,标签<a>中含有其他标签,比如<em>..</em>,此时要提取<a>中的数据,需要用k.get_text()

soup = BeautifulSoup(html, 'html.parser')   #文档对象
#查找a标签,只会查找出一个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

如果,标签<a>中含有其他标签,比如<em>..</em>,此时要提取<a>中的数据,需要用k.get_text()

通常我们使用下面这种模式也是能够处理的,下面的方法使用了get()。

html = urlopen(url)
 soup = BeautifulSoup(html, 'html.parser')
 t1 = soup.find_all('a')
 print t1
 href_list = []
 for t2 in t1:
    t3 = t2.get('href')
    href_list.append(t3)

补充:python爬虫获取任意页面的标签和属性(包括获取a标签的href属性)

看代码吧~

# coding=utf-8 
from bs4 import BeautifulSoup 
import requests 
# 定义一个获取url页面下label标签的attr属性的函数 
def getHtml(url, label, attr): 
    response = requests.get(url) 
    response.encoding = 'utf-8' 
    html = response.text 
    soup = BeautifulSoup(html, 'html.parser'); 
    for target in soup.find_all(label):
 
        try: 
            value = target.get(attr)
 
        except: 
            value = ''
 
        if value: 
            print(value)
 
url = 'https://baidu.com/' 
label = 'a' 
attr = 'href' 
getHtml(url, label, attr)

python 如何获取页面所有a标签下href的值

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。如有错误或未考虑完全的地方,望不吝赐教。

Python 相关文章推荐
用Python从零实现贝叶斯分类器的机器学习的教程
Mar 31 Python
python虚拟环境virualenv的安装与使用
Dec 18 Python
python+ffmpeg视频并发直播压力测试
Mar 06 Python
Python入门学习指南分享
Apr 11 Python
matplotlib subplots 设置总图的标题方法
May 25 Python
Python在for循环中更改list值的方法【推荐】
Aug 17 Python
Python-接口开发入门解析
Aug 01 Python
Python解析多帧dicom数据详解
Jan 13 Python
python raise的基本使用
Sep 10 Python
python 解决函数返回return的问题
Dec 05 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
Jan 15 Python
python模拟浏览器 使用selenium进入好友QQ空间并留言
Apr 12 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
Breeze 文章管理系统 v1.0.0正式发布
2006/12/14 PHP
PHP输出数组中重名的元素的几种处理方法
2012/09/05 PHP
简单的php数据库操作类代码(增,删,改,查)
2013/04/08 PHP
PHP模拟post提交数据方法汇总
2016/02/16 PHP
php bootstrap实现简单登录
2016/03/08 PHP
PHP微信开发用Cache 解决数据缓存
2016/07/11 PHP
Zend Framework使用Zend_Loader组件动态加载文件和类用法详解
2016/12/09 PHP
基于jsTree的无限级树JSON数据的转换代码
2010/07/27 Javascript
jQuery.getScript加载同域JS的代码
2012/02/13 Javascript
Javascript this 的一些学习总结
2012/08/02 Javascript
如何使用jQuery Draggable和Droppable实现拖拽功能
2013/07/05 Javascript
jQuery下实现等待指定元素加载完毕(可改成纯js版)
2013/07/11 Javascript
javascript数组随机排序实例分析
2015/07/22 Javascript
js下将金额数字每三位一逗号分隔
2016/02/19 Javascript
jquery ezUI 双击行记录弹窗查看明细的实现方法
2016/06/01 Javascript
Vue系列:通过vue-router如何传递参数示例
2017/01/16 Javascript
微信小程序联网请求的轮播图
2017/07/07 Javascript
Node使用Sequlize连接Mysql报错:Access denied for user ‘xxx’@‘localhost’
2018/01/03 Javascript
原生JS实现自定义下拉单选选择框功能
2018/10/12 Javascript
JS防抖和节流实例解析
2019/09/24 Javascript
Python对两个有序列表进行合并和排序的例子
2014/06/13 Python
python用来获得图片exif信息的库实例分析
2015/03/16 Python
python3实现全角和半角字符转换的方法示例
2017/09/21 Python
利用python为运维人员写一个监控脚本
2018/03/25 Python
Django REST framework视图的用法
2019/01/16 Python
解决安装pycharm后不能执行python脚本的问题
2019/01/19 Python
Python下opencv图像阈值处理的使用笔记
2019/08/04 Python
python代码xml转txt实例
2020/03/10 Python
htnl5利用svg页面高斯模糊的方法
2018/07/20 HTML / CSS
英国快时尚女装购物网站:PrettyLittleThing
2018/08/15 全球购物
大学本科毕业生的自我鉴定范文
2013/11/19 职场文书
护士节策划方案
2014/05/19 职场文书
建筑安全生产目标责任书
2014/07/23 职场文书
教师自查自纠工作情况报告
2014/10/29 职场文书
会计工作检讨书
2015/02/19 职场文书
Python+SeaTable实现计算两个日期间的工作日天数
2022/07/07 Python