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中的应用之translate和maketrans用法详解
Aug 27 Python
让Python代码更快运行的5种方法
Jun 21 Python
Python+Pika+RabbitMQ环境部署及实现工作队列的实例教程
Jun 29 Python
Python使用正则表达式抓取网页图片的方法示例
Apr 21 Python
新手如何快速入门Python(菜鸟必看篇)
Jun 10 Python
Python设计模式之简单工厂模式实例详解
Jan 22 Python
python实践项目之监控当前联网状态详情
May 23 Python
使用Python脚本zabbix自定义key监控oracle连接状态
Aug 28 Python
Python 实现Image和Ndarray互相转换
Feb 19 Python
Python基础之字符串常见操作经典实例详解
Feb 26 Python
python的scipy.stats模块中正态分布常用函数总结
Feb 19 Python
Django模型层实现多表关系创建和多表操作
Jul 21 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
PHP print类函数使用总结
2010/06/25 PHP
php实现文件下载功能的几个代码分享
2014/05/10 PHP
传智播客学习之JavaScript基础篇
2009/11/13 Javascript
JSON 数据格式介绍
2012/01/13 Javascript
一个检测表单数据的JavaScript实例
2014/10/31 Javascript
js检测iframe是否加载完成的方法
2015/11/26 Javascript
同步文本框内容JS代码实现
2016/08/04 Javascript
移动端脚本框架Hammer.js
2016/12/15 Javascript
从源码看angular/material2 中 dialog模块的实现方法
2017/10/18 Javascript
利用ES6实现单例模式及其应用详解
2017/12/09 Javascript
vue.js做一个简单的编辑菜谱功能
2018/05/08 Javascript
JavaScript引用类型Array实例分析
2018/07/24 Javascript
React 组件中的 bind(this)示例代码
2018/09/16 Javascript
实用Javascript调试技巧分享(小结)
2019/06/18 Javascript
三分钟教你用Node做一个微信哄女友(基友)神器(面向小白)
2019/06/21 Javascript
JS的时间格式化和时间戳转换函数示例详解
2020/07/27 Javascript
深入了解Vue动态组件和异步组件
2021/01/26 Vue.js
Python中装饰器的一个妙用
2015/02/08 Python
python采集百度百科的方法
2015/06/05 Python
Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
2017/04/17 Python
Python复制Word内容并使用格式设字体与大小实例代码
2018/01/22 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
2018/02/08 Python
Python在for循环中更改list值的方法【推荐】
2018/08/17 Python
Appium+python自动化之连接模拟器并启动淘宝APP(超详解)
2019/06/17 Python
在pycharm下设置自己的个性模版方法
2019/07/15 Python
django框架两个使用模板实例
2019/12/11 Python
Python打印特殊符号及对应编码解析
2020/05/07 Python
html5 touch事件实现触屏页面上下滑动(二)
2016/03/10 HTML / CSS
IE9下html5初试小刀
2010/09/21 HTML / CSS
创意广告词
2014/03/17 职场文书
小学优秀班主任事迹材料
2014/05/17 职场文书
九一八事变纪念日演讲稿
2014/09/14 职场文书
生活小常识广播稿
2015/08/19 职场文书
班主任工作经验交流会总结
2015/11/02 职场文书
《月球之谜》教学反思
2016/02/20 职场文书
Golang数据类型和相互转换
2022/04/12 Golang