3 回答

TA貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊
您可以首先通過將標(biāo)簽設(shè)置為BeautifulSoup對象來獲取 url?。如果它已經(jīng)是一個 BeautifulSoup 對象那么你可以直接應(yīng)用它
.find("a").get("href")
如果沒有,那么您可以將其設(shè)為 BeautifulSoup 對象。
from bs4 import BeautifulSoup #pip install beautifulsoup4
a_tag ='<a rel="nofollow">Twitter for iPhone</a>'
soup = BeautifulSoup(a_tag,"html5lib") #pip install html5lib
print(soup.find("a").get("href"))
#output - > http://twitter.com/download/iphone
然后用這個函數(shù)去掉html,文字就剩下了
import re
def remove_html_tags(raw_html):
? ? cleanr = re.compile("<.*?>")
? ? clean_text = re.sub(cleanr,'',raw_html)
? ? return clean_text
output = remove_html_tags(a_tag)
print(output)
#output -> Twitter for iPhone

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊
您可以使用 python?urlextract模塊從任何字符串中提取 URL -
from urlextract import URLExtract
text = '''
<a rel="nofollow">Twitter for iPhone</a>
'''
text = text.replace(' ', '').replace('=','')
extractor = URLExtract()
print(extractor.find_urls(text))
輸出-
['http://twitter.com/download/iphone']

TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊
您可以拆分“”。并獲取第二個元素。
.split('"')[1]
https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split
添加回答
舉報