Python读取本地文件并解析网页元素的方法


Posted in Python onMay 21, 2018

如下所示:

from bs4 import BeautifulSoup
path = './web/new_index.html'
with open(path, 'r') as f:
 Soup = BeautifulSoup(f.read(), 'lxml')
 titles = Soup.select('ul > li > div.article-info > h3 > a')
for title in titles:
 print(title.text)

输出:
Sardinia's top 10 beaches
How to get tanned
How to be an Aussie beach bum
Summer's cheat sheet
#其中
titles = Soup.select('ul > li > div.article-info > h3 > a')
#等效
titles = Soup.select('h3 a')
print(title.text)
#等效
print(title.get_text())
print(title.string)

也可以使用以下代码

import bs4 
 
path = './web/new_index.html' 
 
with open(path, 'r') as f: 
 Soup = bs4.BeautifulSoup(f.read(), 'lxml') 
 
 titles = Soup.select('h3 a') 
for title in titles: 
 print(title.string)

Html原文:

<html>
<head>
 <link rel="stylesheet" type="text/css" href="new_blah.css" rel="external nofollow" >
</head>
<body>
 <div class="header">
  <img src="images/blah.png">
  <ul class="nav">
   <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Home</a></li>
   <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Site</a></li>
   <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Other</a></li>
  </ul>
 </div>
 <div class="main-content">
  <h2>Article</h2>
  <ul class="articles">
   <li>
    <img src="images/0001.jpg" width="100" height="91">
    <div class="article-info">
     <h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Sardinia's top 10 beaches</a></h3>
     <p class="meta-info">
      <span class="meta-cate">fun</span>
      <span class="meta-cate">Wow</span>
     </p>
     <p class="description">white sands and turquoise waters</p>
    </div>
    <div class="rate">
     <span class="rate-score">4.5</span>
    </div>
   </li>
   <li>
    <img src="images/0002.jpg" width="100" height="91">
    <div class="article-info">
     <h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >How to get tanned</a></h3>
     <p class="meta-info">
      <span class="meta-cate">butt</span><span class="meta-cate">NSFW</span>
     </p>
     <p class="description">hot bikini girls on beach</p>
    </div>
    <div class="rate">
     <img src="images/Fire.png" width="18" height="18">
     <span class="rate-score">5.0</span>
    </div>
   </li>
   <li>
    <img src="images/0003.jpg" width="100" height="91">
    <div class="article-info">
     <h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >How to be an Aussie beach bum</a></h3>
     <p class="meta-info">
      <span class="meta-cate">sea</span>
     </p>
     <p class="description">To make the most of your visit</p>
    </div>
    <div class="rate">
     <span class="rate-score">3.5</span>
    </div>
   </li>
   <li>
    <img src="images/0004.jpg" width="100" height="91">
    <div class="article-info">
     <h3><a href="www.sample.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Summer's cheat sheet</a></h3>
     <p class="meta-info">
      <span class="meta-cate">bay</span>
      <span class="meta-cate">boat</span>
      <span class="meta-cate">beach</span>
     </p>
     <p class="description">choosing a beach in Cape Cod</p>
    </div>
    <div class="rate">
     <span class="rate-score">3.0</span>
    </div>
   </li>
  </ul>
 </div>
 <div class="footer">
  <p>© Mugglecoding</p>
 </div>
</body>
</html>

以上这篇Python读取本地文件并解析网页元素的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
基于Python Shell获取hostname和fqdn释疑
Jan 25 Python
Python利用itchat对微信中好友数据实现简单分析的方法
Nov 21 Python
Python断言assert的用法代码解析
Feb 03 Python
wxpython实现图书管理系统
Mar 12 Python
Python 循环语句之 while,for语句详解
Apr 23 Python
Python开发虚拟环境使用virtualenvwrapper的搭建步骤教程图解
Sep 19 Python
python 使用正则表达式按照多个空格分割字符的实例
Dec 20 Python
对python函数签名的方法详解
Jan 22 Python
我用Python抓取了7000 多本电子书案例详解
Mar 25 Python
Python如何在main中调用函数内的函数方式
Jun 01 Python
python3爬虫中异步协程的用法
Jul 10 Python
python中pymysql包操作数据库方法
Apr 19 Python
详解Python中的四种队列
May 21 #Python
Python实现的当前时间多加一天、一小时、一分钟操作示例
May 21 #Python
Python自定义函数实现求两个数最大公约数、最小公倍数示例
May 21 #Python
Python基于递归和非递归算法求两个数最大公约数、最小公倍数示例
May 21 #Python
Python常用字符串替换函数strip、replace及sub用法示例
May 21 #Python
Python下使用Scrapy爬取网页内容的实例
May 21 #Python
python 每天如何定时启动爬虫任务(实现方法分享)
May 21 #Python
You might like
php获取post中的json数据的实现方法
2011/06/08 PHP
php 浮点数比较方法详解
2017/05/05 PHP
深入学习微信网址链接解封的防封原理visit_type
2019/08/15 PHP
php获取是星期几的的一些常用姿势
2019/12/15 PHP
关于PHP求解三数之和问题详析
2020/11/09 PHP
php实现简单四则运算器
2020/11/29 PHP
javascript XML数据显示为HTML一例
2008/12/23 Javascript
javascript操作html控件实例(javascript添加html)
2013/12/02 Javascript
js简单的点击返回顶部效果实现方法
2015/04/10 Javascript
javascript实现根据3原色制作颜色选择器的方法
2015/07/17 Javascript
浅谈Javascript中substr和substring的区别
2015/09/30 Javascript
js实现的光标位置工具函数示例
2016/10/03 Javascript
JS实现页面进入和返回定位到具体位置
2016/12/08 Javascript
AngularJS的Filter的示例详解
2017/03/07 Javascript
jQuery除指定区域外点击任何地方隐藏DIV功能
2017/11/13 jQuery
使用vuex缓存数据并优化自己的vuex-cache
2018/05/30 Javascript
微信小程序之自定义组件的实现代码(附源码)
2018/08/02 Javascript
vue iview的菜单组件Mune 点击不高亮的解决方案
2019/11/01 Javascript
node.js 如何监视文件变化
2020/09/01 Javascript
Python不规范的日期字符串处理类
2014/06/10 Python
使用python读取txt文件的内容,并删除重复的行数方法
2018/04/18 Python
Django csrf 两种方法设置form的实例
2019/02/03 Python
在python Numpy中求向量和矩阵的范数实例
2019/08/26 Python
Python调用Windows API函数编写录音机和音乐播放器功能
2020/01/05 Python
Python通过kerberos安全认证操作kafka方式
2020/06/06 Python
学python最电脑配置有要求么
2020/07/05 Python
美国批发供应商:Kole Imports
2019/04/10 全球购物
EJB面试题
2015/07/28 面试题
素食餐饮项目创业计划书
2014/02/02 职场文书
班组拓展活动方案
2014/08/14 职场文书
大学军训口号大全
2015/12/24 职场文书
坚持不是死撑,更重要的是心态
2019/08/19 职场文书
85句关于理想的名言警句大全
2019/08/22 职场文书
goland 恢复已更改文件的操作
2021/04/28 Golang
vscode中使用npm安装babel的方法
2021/08/02 Javascript
一文搞懂Golang 时间和日期相关函数
2021/12/06 Golang