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的装饰器模式与面向切面编程详解
Jun 21 Python
Python 内置函数complex详解
Oct 23 Python
Python实现判断一个字符串是否包含子串的方法总结
Nov 21 Python
用Python实现KNN分类算法
Dec 22 Python
python3.4.3下逐行读入txt文本并去重的方法
Apr 29 Python
python多线程并发让两个LED同时亮的方法
Feb 18 Python
Python中判断子串存在的性能比较及分析总结
Jun 23 Python
django Admin文档生成器使用详解
Jul 22 Python
Python sys模块常用方法解析
Feb 20 Python
Django前后端分离csrf token获取方式
Dec 25 Python
解决python的空格和tab混淆而报错的问题
Feb 26 Python
分享Python异步爬取知乎热榜
Apr 12 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
PHP学习之PHP运算符
2006/10/09 PHP
php2html php生成静态页函数
2008/12/08 PHP
php教程之魔术方法的使用示例(php魔术函数)
2014/02/12 PHP
php随机显示图片的简单示例
2014/02/15 PHP
通过修改配置真正解决php文件上传大小限制问题(nginx+php)
2015/09/23 PHP
使用Huagepage和PGO来提升PHP7的执行性能
2015/11/30 PHP
PHP实现字符串翻转功能的方法【递归与循环算法】
2017/11/03 PHP
php加速缓存器opcache,apc,xcache,eAccelerator原理与配置方法实例分析
2020/03/02 PHP
jQuery 源码分析笔记(6) jQuery.data
2011/06/08 Javascript
兼容ie、firefox的图片自动缩放的css跟js代码分享
2012/01/21 Javascript
解决js中window.open弹出的是上次的缓存页面问题
2013/12/29 Javascript
采用自执行的匿名函数解决for循环使用闭包的问题
2014/09/11 Javascript
vue2.0数据双向绑定与表单bootstrap+vue组件
2017/02/27 Javascript
jQuery实现页码跳转式动态数据分页
2017/12/31 jQuery
vue实现输入一位数字转汉字功能
2019/12/13 Javascript
Nodejs文件上传、监听上传进度的代码
2020/03/27 NodeJs
vue+echarts实现动态折线图的方法与注意
2020/09/01 Javascript
python解析xml文件操作实例
2014/10/05 Python
Django1.7+python 2.78+pycharm配置mysql数据库教程
2014/11/18 Python
python字符串的常用操作方法小结
2016/05/21 Python
Python编程实现双击更新所有已安装python模块的方法
2017/06/05 Python
python实现字符串和字典的转换
2018/09/29 Python
mac系统下Redis安装和使用步骤详解
2019/07/09 Python
Python中turtle库的使用实例
2019/09/09 Python
使用python实现画AR模型时序图
2019/11/20 Python
python3.7添加dlib模块的方法
2020/07/01 Python
Python基于unittest实现测试用例执行
2020/11/25 Python
俄罗斯香水和化妆品购物网站:Л’Этуаль
2018/05/10 全球购物
经贸日语专业个人求职信范文
2013/12/28 职场文书
党在我心中的演讲稿
2014/09/13 职场文书
学习走群众路线心得体会
2014/11/05 职场文书
2014年办公室工作总结范文
2014/11/12 职场文书
趵突泉导游词
2015/02/03 职场文书
公司借条范本
2015/05/25 职场文书
2015年市场营销工作总结
2015/07/23 职场文书
nginx 多个location转发任意请求或访问静态资源文件的实现
2021/03/31 Servers