logo资料库

BeautifulSoup 获取 a标签里的文本内容.pdf

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
BeautifulSoup 获取获取 a标签里的文本内容 标签里的文本内容 说明说明 想要获取 a标签里的单词如下所示。 代码代码 from bs4 import BeautifulSoup f = open("word.txt", "r") # 设置文件对象 html = f.read() # 将txt文件的所有内容读入到字符串html中 soup = BeautifulSoup(html, 'lxml') # 获取a标签里的文本内容 for item in soup.find_all("a"): print(item.string) # 将单词写入five_star.txt 文件 with open('five_star.txt', 'a', encoding='utf-8') as file: file.write(item.string + '\n') f.close() # 将文件关闭 word.txt 存放的是 a标签所有内容(内容过多 故部分展示) five_star.txt 输出结果(内容过多 故部分展示)
作者:无梦生7
分享到:
收藏