当前位置: 移动技术网 > IT编程>脚本编程>Python > Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例

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

2019年07月21日  | 移动技术网IT编程  | 我要评论

光华网,薄熙来谋反,婴适康

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《python进程与线程操作技巧总结》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》、《python入门与进阶经典教程》、《python+mysql数据库程序设计入门教程》及《python常见数据库操作技巧汇总

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网