Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例
本文实例讲述了Python获取基金网站网页内容、使用BeautifulSoup库分析html操作。分享给大家供大家参考,具体如下:
利用urllib包获取网页内容
#引入包
fromurllib.requestimporturlopen
response=urlopen("http://fund.eastmoney.com/fund.html")
html=response.read();
#这个网页编码是gb2312
#print(html.decode("gb2312"))
#把html内容保存到一个文件
withopen("1.txt","wb")asf:
f.write(html.decode("gb2312").encode("utf8"))
f.close()
使用BeautifulSoup分析html
frombs4importBeautifulSoup
#读取文件内容
withopen("1.txt","rb")asf:
html=f.read().decode("utf8")
f.close()
#分析html内容
soup=BeautifulSoup(html,"html.parser")
#取出网页title
print(soup.title)#每日开放式基金净值表_天天基金网
#基金编码
codes=soup.find("table",id="oTable").tbody.find_all("td","bzdm")
result=()#初始化一个元组
forcodeincodes:
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程序设计有所帮助。
热门推荐
1 训练狗狗搜寻
2 如何训练狗狗装死
3 狗狗训练书
6 怎么训练狗狗拜年
8 狗狗训练游戏
10 狗狗嗅觉训练
11 怎么训练狗狗在笼子里不叫
12 怎么训练狗狗站立走路
13 怎么训练狗狗在马桶上
14 狗狗尿尿怎么训练
15 训练狗狗用品
16 泰迪狗狗的训练方法大小便
17 训练狗狗可以打吗
18 王曼丽的狗狗训练