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 24 Python
在Python中使用__slots__方法的详细教程
Apr 28 Python
Python简单连接MongoDB数据库的方法
Mar 15 Python
Python+matplotlib+numpy实现在不同平面的二维条形图
Jan 02 Python
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
Apr 18 Python
python+webdriver自动化环境搭建步骤详解
Jun 03 Python
Python 下载及安装详细步骤
Nov 04 Python
Python换行与不换行的输出实例
Feb 19 Python
Python使用GitPython操作Git版本库的方法
Feb 29 Python
python GUI库图形界面开发之PyQt5信号与槽事件处理机制详细介绍与实例解析
Mar 08 Python
使用Python内置模块与函数进行不同进制的数的转换
Apr 26 Python
django restframework serializer 增加自定义字段操作
Jul 15 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
比较discuz和ecshop的截取字符串函数php版
2012/09/03 PHP
使用迭代器 遍历文件信息的详解
2013/06/08 PHP
PHP扩展开发教程(总结)
2015/11/04 PHP
php用户注册信息验证正则表达式
2015/11/12 PHP
一个简单的php MVC留言本实例代码(必看篇)
2016/09/22 PHP
详解PHP使用Redis存储session时的一个Warning定位
2017/07/05 PHP
Laravel中GraphQL接口请求频率实战记录
2020/09/01 PHP
IE 条件注释详解总结(附实例代码)
2009/08/29 Javascript
js 跳出页面的frameset框架示例介绍
2013/12/23 Javascript
jQuery替换textarea中换行的方法
2015/06/10 Javascript
javascript创建动态表单的方法
2015/07/25 Javascript
JavaScript的Polymer框架中dom-repeat与VM的相关操作
2015/07/29 Javascript
jQuery Validate验证框架经典大全
2015/09/23 Javascript
Koa项目搭建过程详细记录
2018/04/12 Javascript
vue-router判断页面未登录自动跳转到登录页的方法示例
2018/11/04 Javascript
js实现蒙版效果
2020/01/11 Javascript
js实现可爱的气泡特效
2020/09/05 Javascript
[01:21:36]CHAOS vs Alliacne 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
在Python中测试访问同一数据的竞争条件的方法
2015/04/23 Python
python查询sqlite数据表的方法
2015/05/08 Python
在Django中同时使用多个配置文件的方法
2015/07/22 Python
Python 如何访问外围作用域中的变量
2016/09/11 Python
Python实现将HTML转换成doc格式文件的方法示例
2017/11/20 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
2018/07/18 Python
python实现根据文件关键字进行切分为多个文件的示例
2018/12/10 Python
python实现比对美团接口返回数据和本地mongo数据是否一致示例
2019/08/09 Python
Python中生成一个指定长度的随机字符串实现示例
2019/11/06 Python
Python 实现OpenCV格式和PIL.Image格式互转
2020/01/09 Python
信号生成及DFT的python实现方式
2020/02/25 Python
自动化专业个人求职信范文
2013/11/29 职场文书
致跳远、跳高运动员广播稿
2014/01/09 职场文书
护士的自我鉴定
2014/02/07 职场文书
人力资源管理专业自荐书范文
2014/02/10 职场文书
内乡县衙导游词
2015/02/05 职场文书
python随机打印成绩排名表
2021/06/23 Python
python数据可视化JupyterLab实用扩展程序Mito
2021/11/20 Python