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 限制函数调用次数的实例讲解
Apr 21 Python
python 3.7.0 下pillow安装方法
Aug 27 Python
python排序函数sort()与sorted()的区别
Sep 18 Python
widows下安装pycurl并利用pycurl请求https地址的方法
Oct 15 Python
python 递归深度优先搜索与广度优先搜索算法模拟实现
Oct 22 Python
python爬虫URL重试机制的实现方法(python2.7以及python3.5)
Dec 18 Python
Python3远程监控程序的实现方法
Jul 15 Python
python3实现单目标粒子群算法
Nov 14 Python
opencv设置采集视频分辨率方式
Dec 10 Python
Python3爬虫关于代理池的维护详解
Jul 30 Python
python抢购软件/插件/脚本附完整源码
Mar 04 Python
在Python中如何使用yield
Jun 07 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
PHP4实际应用经验篇(7)
2006/10/09 PHP
php实现把url转换迅雷thunder资源下载地址的方法
2014/11/07 PHP
微信支付PHP SDK ―― 公众号支付代码详解
2016/09/13 PHP
解决laravel session失效的问题
2019/10/14 PHP
如何做到打开一个页面,过几分钟自动转到另一页面
2007/04/20 Javascript
JS验证身份证有效性示例
2013/10/11 Javascript
Javascript中浮点数相乘的一个解决方法
2014/06/03 Javascript
jquery操作对象数组元素方法详解
2014/11/26 Javascript
AngularJS的表单使用详解
2015/06/17 Javascript
javascript实现跨域的方法汇总
2015/06/25 Javascript
iScroll.js 使用方法参考
2016/05/16 Javascript
JavaScript对象数组如何按指定属性和排序方向进行排序
2016/06/15 Javascript
jQuery Ajax File Upload实例源码
2016/12/12 Javascript
浅析JavaScript的几种Math函数,random(),ceil(),round(),floor()
2016/12/22 Javascript
用最少的JS代码写出贪吃蛇游戏
2018/01/12 Javascript
Python读写ini文件的方法
2015/05/28 Python
同时安装Python2 &amp; Python3 cmd下版本自由选择的方法
2017/12/09 Python
python timestamp和datetime之间转换详解
2017/12/11 Python
pytorch 调整某一维度数据顺序的方法
2018/12/08 Python
详解Python3中ceil()函数用法
2019/02/19 Python
Django中使用Whoosh进行全文检索的方法
2019/03/31 Python
Python英文文章词频统计(14份剑桥真题词频统计)
2019/10/13 Python
python写一个随机点名软件的实例
2019/11/28 Python
AUC计算方法与Python实现代码
2020/02/28 Python
Python爬取梨视频的示例
2021/01/29 Python
HTML5 Convas APIs方法详解
2015/04/24 HTML / CSS
怎样实现H5+CSS3手指滑动切换图片的示例代码
2019/05/05 HTML / CSS
美国排名第一的葡萄酒俱乐部:Firstleaf Wine Club
2020/01/02 全球购物
消防安全汇报材料
2014/02/08 职场文书
美容院店长岗位职责
2014/04/08 职场文书
逃课打麻将检讨书
2014/10/05 职场文书
实习员工转正的评语汇总,以备不时之需
2019/12/17 职场文书
深度学习小工程练习之垃圾分类详解
2021/04/14 Python
90行Python代码开发个人云盘应用
2021/04/20 Python
Java 超详细讲解数据结构中的堆的应用
2022/04/02 Java/Android
MySQL索引失效十种场景与优化方案
2023/05/08 MySQL