簡體   English   中英

從輸出中刪除HTML標簽

[英]Remove HTML tags from output

我是python的新手,無法從輸出中刪除html標簽。 我想刪除標簽和其中的內容。 我也想刪除p標簽。 有什么建議么?

import urllib2
from bs4 import BeautifulSoup

# Ask user to enter URL
url = raw_input("Please enter a valid URL: ")

# Make sure file is clear for new content
open('ctp_output.txt', 'w').close()

# Open txt document for output
txt = open('ctp_output.txt', 'w')

# Parse HTML of article, aka making soup
soup = BeautifulSoup(urllib2.urlopen(url).read())

# retrieve all of the paragraph tags
tags = soup('p')
txt.write(str(tag) + '\n' + '\n')

# Close txt file with new content added
txt.close()

通過使用get_text()函數而不是字符串表示形式( str(tag) )從標記中檢索文本部分。

在上面的代碼中,更改將替換為以下行:

txt.write(str(tag) + '\n' + '\n')

與:

txt.write(tag.get_text() + '\n' + '\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM