Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例

作者:学习笔记666 时间:2022-10-04 00:34:40 

本文实例讲述了Python获取基金网站网页内容、使用BeautifulSoup库分析html操作。分享给大家供大家参考,具体如下:

利用 urllib包 获取网页内容


#引入包
from urllib.request import urlopen
response = urlopen("http://fund.eastmoney.com/fund.html")
html = response.read();
#这个网页编码是gb2312
#print(html.decode("gb2312"))
#把html内容保存到一个文件
with open("1.txt","wb") as f:
 f.write(html.decode("gb2312").encode("utf8"))
 f.close()

使用BeautifulSoup分析html


from bs4 import BeautifulSoup
# 读取文件内容
with open("1.txt", "rb") as f:
 html = f.read().decode("utf8")
 f.close()
# 分析html内容
soup = BeautifulSoup(html,"html.parser")
# 取出网页title
print(soup.title) #<title>每日开放式基金净值表 _ 天天基金网</title>
# 基金编码
codes = soup.find("table",id="oTable").tbody.find_all("td","bzdm")
result = () # 初始化一个元组
for code in codes:
 result += ({
   "code":code.get_text(),
   "name":code.next_sibling.find("a").get_text(),
   "NAV":code.next_sibling.next_sibling.get_text(),
   "ACCNAV":code.next_sibling.next_sibling.next_sibling.get_text()
  },)
# 打印结果
print(result[0]["name"])

希望本文所述对大家Python程序设计有所帮助。

来源:https://blog.csdn.net/github_26672553/article/details/78529734

标签:Python,网页内容,BeautifulSoup,html
0
投稿

猜你喜欢

  • uniapp实现支付功能

    2023-08-21 13:53:47
  • python装饰器使用方法实例

    2022-12-14 11:24:07
  • SQLSERVER查询所有数据库名,表名,和字段名的语句

    2012-01-29 18:07:44
  • python调用cmd复制文件代码分享

    2022-12-26 11:18:22
  • javascript的正则表达式

    2010-07-27 12:29:00
  • Python设计模式之模板方法模式实例详解

    2023-07-21 01:29:19
  • 深入了解Python数据类型之列表

    2022-12-21 23:14:15
  • python基础教程之基本数据类型和变量声明介绍

    2023-08-28 22:45:55
  • asp对象之:基于adodb.stream的文件操作类

    2008-06-07 08:38:00
  • Python第三方库qrcode或MyQr生成博客地址二维码

    2023-06-25 06:23:08
  • mysql 忘记root密码

    2010-12-14 14:50:00
  • 2007流行网站导航设计欣赏

    2008-02-18 12:20:00
  • Python数据可视化 pyecharts实现各种统计图表过程详解

    2022-04-08 17:28:37
  • 搜索关键字加亮js实现方法

    2007-08-27 14:11:00
  • Python+Opencv身份证号码区域提取及识别实现

    2021-10-01 17:32:13
  • 查看python下OpenCV版本的方法

    2022-12-24 18:15:32
  • 十幅图告诉你什么是PHP引用

    2023-10-04 06:16:56
  • Python量化交易实战之使用Resample函数转换“日K”数据

    2023-07-30 07:31:11
  • Python远程视频监控程序的实例代码

    2021-02-08 14:26:22
  • 设计MySQL数据库的技巧

    2009-09-06 11:56:00
  • asp之家 网络编程 m.aspxhome.com