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实现基于两张图片生成圆角图标效果的方法
Mar 26 Python
PyQt5显示GIF图片的方法
Jun 17 Python
Python3 无重复字符的最长子串的实现
Oct 08 Python
python中property和setter装饰器用法
Dec 19 Python
python获取网络图片方法及整理过程详解
Dec 20 Python
Python序列类型的打包和解包实例
Dec 21 Python
解决python3插入mysql时内容带有引号的问题
Mar 02 Python
selenium+python配置chrome浏览器的选项的实现
Mar 18 Python
基于python实现计算两组数据P值
Jul 10 Python
Django 权限管理(permissions)与用户组(group)详解
Nov 30 Python
python使用scapy模块实现ARP扫描的过程
Jan 21 Python
利用Python网络爬虫爬取各大音乐评论的代码
Apr 13 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
php session 错误
2009/05/21 PHP
PHP项目开发中最常用的自定义函数整理
2010/12/02 PHP
PHP取二进制文件头快速判断文件类型的实现代码
2013/08/05 PHP
编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法
2014/06/27 PHP
php实现的任意进制互转类分享
2015/07/07 PHP
Symfony2使用Doctrine进行数据库查询方法实例总结
2016/03/18 PHP
PHP Socket网络操作类定义与用法示例
2017/08/30 PHP
JS 页面自动加载函数(兼容多浏览器)
2009/05/18 Javascript
怎么清空javascript数组
2013/05/11 Javascript
js判断两个日期是否相等的方法
2013/09/10 Javascript
教你如何自定义百度分享插件以及bshare分享插件的分享按钮
2014/06/20 Javascript
使用百度地图api实现根据地址查询经纬度
2014/12/11 Javascript
jQuery使用animate创建动画用法实例
2015/08/07 Javascript
Javascript仿新浪游戏频道鼠标悬停显示子菜单效果
2015/08/21 Javascript
基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
2015/11/24 Javascript
javascript运算符语法全面概述
2016/07/14 Javascript
JS面试题---关于算法台阶的问题
2016/07/26 Javascript
React.Js添加与删除onScroll事件的方法详解
2017/11/03 Javascript
在vue-cli搭建的项目中增加后台mock接口的方法
2018/04/26 Javascript
微信小程序分享海报生成的实现方法
2018/12/10 Javascript
nodejs实现的http、https 请求封装操作示例
2020/02/06 NodeJs
VUE UPLOAD 通过ACTION返回上传结果操作
2020/09/07 Javascript
python中range()与xrange()用法分析
2016/09/21 Python
如何利用Fabric自动化你的任务
2016/10/20 Python
python利用pandas将excel文件转换为txt文件的方法
2018/10/23 Python
Python进阶之@property动态属性的实现
2019/04/01 Python
python读取大文件越来越慢的原因与解决
2019/08/08 Python
Python多叉树的构造及取出节点数据(treelib)的方法
2019/08/09 Python
Python使用shutil模块实现文件拷贝
2020/07/31 Python
canvas进阶之贝塞尔公式推导与物体跟随复杂曲线的轨迹运动
2018/01/10 HTML / CSS
英国领先的电视购物零售商:Ideal World
2019/03/18 全球购物
Koral官方网站:女性时尚运动服
2019/04/10 全球购物
家庭贫困证明范本(经典版)
2014/09/22 职场文书
Redis如何实现分布式锁
2021/08/23 Redis
关于MySQL中的 like操作符详情
2021/11/17 MySQL
一文搞清楚MySQL count(*)、count(1)、count(col)区别
2022/03/03 MySQL