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在多玩图片上下载妹子图的实现代码
Aug 13 Python
Python设置Socket代理及实现远程摄像头控制的例子
Nov 13 Python
Python使用pylab库实现画线功能的方法详解
Jun 08 Python
python difflib模块示例讲解
Sep 13 Python
Python复制Word内容并使用格式设字体与大小实例代码
Jan 22 Python
python+pyqt5实现KFC点餐收银系统
Jan 24 Python
python版DDOS攻击脚本
Jun 12 Python
python manage.py runserver流程解析
Nov 08 Python
python wav模块获取采样率 采样点声道量化位数(实例代码)
Jan 22 Python
五种Python转义表示法
Nov 27 Python
Python爬虫自动化获取华图和粉笔网站的错题(推荐)
Jan 08 Python
详解Python openpyxl库的基本应用
Feb 26 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
php fckeditor 调用的函数
2009/06/21 PHP
php下使用curl模拟用户登陆的代码
2010/09/10 PHP
ThinkPHP的模版中调用session数据的方法
2014/07/01 PHP
php5.5使用PHPMailer-5.2发送邮件的完整步骤
2018/10/14 PHP
php和nginx交互实例讲解
2019/09/24 PHP
用JS控制回车事件的代码
2011/02/20 Javascript
兼容IE、firefox以及chrome的js获取时间(getFullYear)
2014/07/04 Javascript
javascript使用数组的push方法完成快速排序
2014/09/15 Javascript
浅谈Javascript中的Function与Object
2015/01/26 Javascript
jQuery实现移动 和 渐变特效的点击事件
2015/02/26 Javascript
js判断手机号运营商的方法
2015/10/23 Javascript
jQuery实现的精美平滑二级下拉菜单效果代码
2016/03/28 Javascript
IE和Firefox之间在JavaScript语法上的差异
2016/04/22 Javascript
深入浅出ES6新特性之函数默认参数和箭头函数
2016/08/01 Javascript
浅谈JavaScript的自动垃圾收集机制
2016/12/15 Javascript
js实现图片放大展示效果
2017/08/30 Javascript
vue jsx 使用指南及vue.js 使用jsx语法的方法
2017/11/11 Javascript
Vue 实例事件简单示例
2019/09/19 Javascript
Vue2.0 实现页面缓存和不缓存的方式
2019/11/12 Javascript
Vue混入mixins滚动触底的方法
2019/11/22 Javascript
[54:06]OG vs TNC 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
[01:11:32]VG vs FNATIC 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
python进阶教程之异常处理
2014/08/30 Python
python通过urllib2获取带有中文参数url内容的方法
2015/03/13 Python
详解Python中映射类型(字典)操作符的概念和使用
2015/08/19 Python
Python猴子补丁知识点总结
2020/01/05 Python
Python tkinter和exe打包的方法
2020/02/05 Python
html5 canvas实现跟随鼠标旋转的箭头
2016/03/11 HTML / CSS
YesStyle美国/全球:购买亚洲时装、美容化妆品和生活百货
2017/01/16 全球购物
Andrew Marc官网:设计师外套的领先制造商
2019/10/30 全球购物
九年级历史教学反思
2014/01/27 职场文书
商务英语专业毕业生求职信
2014/07/06 职场文书
师德师风自我剖析材料
2014/09/27 职场文书
2015年后勤工作总结范文
2015/04/08 职场文书
CSS实现隐藏搜索框功能(动画正反向序列)
2021/07/21 HTML / CSS
SpringBoot+VUE实现数据表格的实战
2021/08/02 Java/Android