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中readline判断文件读取结束的方法
Nov 08 Python
用Python操作字符串之rindex()方法的使用
May 19 Python
如何处理Python3.4 使用pymssql 乱码问题
Jan 08 Python
ubuntu环境下python虚拟环境的安装过程
Jan 07 Python
windows10下python3.5 pip3安装图文教程
Apr 02 Python
使用Python进行防病毒免杀解析
Dec 13 Python
基于Python获取docx/doc文件内容代码解析
Feb 17 Python
python之MSE、MAE、RMSE的使用
Feb 24 Python
PyCharm 2020 激活到 2100 年的教程
Mar 25 Python
Python unittest单元测试openpyxl实现过程解析
May 27 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
Oct 30 Python
python利用pytesseract 实现本地识别图片文字
Dec 14 Python
Python中常见的导入方式总结
May 06 #Python
Python基础之hashlib模块详解
May 06 #Python
用Python爬虫破解滑动验证码的案例解析
python本地文件服务器实例教程
python字符串常规操作大全
python自动化之如何利用allure生成测试报告
python使用openpyxl库读写Excel表格的方法(增删改查操作)
You might like
PHP 学习路线与时间表
2010/02/21 PHP
PHP设计模式之适配器模式定义与用法详解
2018/04/03 PHP
PHP ADODB实现分页功能简单示例
2018/05/25 PHP
基于Jquery的标签智能验证实现代码
2010/12/27 Javascript
基于JavaScript代码实现兼容各浏览器的设为首页和加入收藏
2016/01/07 Javascript
js中常用的Tab切换效果(推荐)
2016/08/30 Javascript
canvas绘制多边形
2017/02/24 Javascript
bootstrap timepicker在angular中取值并转化为时间戳
2017/06/13 Javascript
详解Vue.js搭建路由报错 router.map is not a function
2017/06/27 Javascript
nodejs 图片预览和上传的示例代码
2017/09/30 NodeJs
谈谈vue中mixin的一点理解
2017/12/12 Javascript
jquery实现楼层滚动效果
2018/01/01 jQuery
Vuejs在v-for中,利用index来对第一项添加class的方法
2018/03/03 Javascript
浅谈在node.js进入文件目录的问题
2018/05/13 Javascript
JavaScript工具库之Lodash详解
2019/06/15 Javascript
angular异步验证防抖踩坑实录
2019/12/01 Javascript
JavaScript实现简单进度条效果
2020/03/25 Javascript
Python实现的简单算术游戏实例
2015/05/26 Python
python跳过第一行快速读取文件内容的实例
2018/07/12 Python
Python当中的array数组对象实例详解
2019/06/12 Python
kafka-python 获取topic lag值方式
2019/12/23 Python
python为QT程序添加图标的方法详解
2020/03/09 Python
教你使用Sublime text3搭建Python开发环境及常用插件安装另分享Sublime text3最新激活注册码
2020/11/12 Python
HTML5 在canvas中绘制文本附效果图
2014/06/23 HTML / CSS
西班牙电子产品购物网站:Electronicamente
2018/07/26 全球购物
专业毕业生个性的自我评价
2013/10/03 职场文书
《都江堰》教学反思
2014/02/07 职场文书
乡镇总工会学雷锋活动总结
2014/03/01 职场文书
《大自然的语言》教学反思
2014/04/08 职场文书
《少年王冕》教学反思
2014/04/11 职场文书
小学生感恩演讲稿
2014/04/25 职场文书
新党章心得体会
2014/09/04 职场文书
委托书的写法
2014/09/16 职场文书
安全保证书怎么写
2015/02/28 职场文书
2015年端午节活动方案
2015/05/05 职场文书
大学组织委员竞选稿
2015/11/21 职场文书