![](/img/trans.png)
[英]I need to add some string to specific parts of a txt file in python
[英]How can I access specific parts of a string in a txt file with python?
所以我有一個包含很多 HTML 行的大文本文件,它是由網絡爬蟲很好地創建的。 它充滿了看起來像下面的代碼的行。 我想知道,我怎樣才能得到一個新的文本文件,其中只包含“所需文本”而不是整行 html 代碼?
b'<b><a href="example.html" target="_blank">Desired Text 1</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 2</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 3</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 4</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 5</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 6</a></b>'
看看 BeautifulSoup,這些例子有一個關於這個問題的演示:
[編輯] 附上您案例的詳細解決方案:
from bs4 import BeautifulSoup
text = """
b'<b><a href="example.html" target="_blank">Desired Text 1</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 2</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 3</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 4</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 5</a></b>'
b'<b><a href="example.html" target="_blank">Desired Text 6</a></b>'
"""
soup = BeautifulSoup(text, 'html.parser')
print soup.getText()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.