3 回答
![?](http://img1.sycdn.imooc.com/545868cd00013bbb02200220-100-100.jpg)
TA贡献1942条经验 获得超3个赞
您可以首先通过将标签设置为BeautifulSoup对象来获取 url 。如果它已经是一个 BeautifulSoup 对象那么你可以直接应用它
.find("a").get("href")
如果没有,那么您可以将其设为 BeautifulSoup 对象。
from bs4 import BeautifulSoup #pip install beautifulsoup4
a_tag ='<a href="http://twitter.com/download/iphone" 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
然后用这个函数去掉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
![?](http://img1.sycdn.imooc.com/5333a1920001d36402200220-100-100.jpg)
TA贡献1859条经验 获得超6个赞
您可以使用 python urlextract模块从任何字符串中提取 URL -
from urlextract import URLExtract
text = '''
<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>
'''
text = text.replace(' ', '').replace('=','')
extractor = URLExtract()
print(extractor.find_urls(text))
输出-
['http://twitter.com/download/iphone']
![?](http://img1.sycdn.imooc.com/54586453000163bd02200220-100-100.jpg)
TA贡献1852条经验 获得超7个赞
您可以拆分“”。并获取第二个元素。
.split('"')[1]
https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split
添加回答
举报