失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python 抓取网页 库_Python获取基金网站网页内容 使用BeautifulSoup库分析html操作示例...

python 抓取网页 库_Python获取基金网站网页内容 使用BeautifulSoup库分析html操作示例...

时间:2024-02-27 11:22:54

相关推荐

python 抓取网页 库_Python获取基金网站网页内容 使用BeautifulSoup库分析html操作示例...

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

利用 urllib包 获取网页内容

#引入包

from urllib.request import urlopen

response = urlopen("/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) #

每日开放式基金净值表 _ 天天基金网

# 基金编码

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获取基金网站网页内容 使用BeautifulSoup库分析html操作示例...》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。