0
一個html文件,我有這個HTML文件:解析使用BeautifulSoup
<html>
<head></head>
<body>
Text1
Text2
<a href="XYCL7Q.html">
Text3
</a>
</body>
</html>
我想收集分別文本1,文本2和文本3。對於Text3我沒有問題,但我無法捕獲Text1-2;通過這樣做:
from urllib import urlopen
from bs4 import BeautifulSoup
url = 'myUrl';
html = urlopen(url).read()
soup = BeautifulSoup(html)
soup.body.get_text()
我得到的所有文本(第一個問題,因爲我得到的文本3再次)沒有得到很好的分離,因爲Text1-2可能包含一些空間......舉例來說,如果文本1是「世界你好」和Text2「foo bar」,最後我想列出2個字符串:
results = ['hello world', 'foo bar']
我該怎麼做?謝謝你的回答...