且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用BeautifulSoup/Python从html文件中提取文本

更新时间:2023-02-19 09:33:40

find_all 方法返回一个列表.遍历列表以获取文本.

The find_all method returns a list. Iterate over the list to get the text.

for name in names:
    print(name.text)

返回:

Baden-Württemberg
Bayern
Berlin

内置的python dir() type()方法总是很方便地检查对象.

The builtin python dir() and type() methods are always handy to inspect an object.

print(dir(names))

[...,
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort',
 'source']