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

  • Post author:
  • Post category:其他



BeautifulSoup 获取 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) # item.string
    # 将单词写入five_star.txt 文件
    with open('five_star.txt', 'a', encoding='utf-8') as file:
        file.write(item.string + '\n')

f.close()  # 将文件关闭

————————————————

版权声明:本文为CSDN博主「无梦生7」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/s1156605343/article/details/105312349