1 回答

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
OP:開頭必須是 href="/wiki/ 中間可以是任何內(nèi)容,結(jié)尾必須是 "
st = "since-OP-did-not-provide-a-sample-string-34278234$'blahhh-okay-enough.href='/wiki/anything/everything/nothing'okay-bye"
print(st[st.find('href'):st.rfind("'")+1])
輸出:
href='/wiki/anything/everything/nothing'
編輯:
如果我們要解析可能的 html,我會(huì)選擇 BeautifulSoup 。
from bs4 import BeautifulSoup
text = '''<a href='/wiki/anything/everything/nothing'><img src="/hp_imgjhg/411/1/f_1hj11_100u.jpg" alt="dyufg" />well wait now <a href='/wiki/hello/how-about-now/nothing'>'''
soup = BeautifulSoup(text, features="lxml")
for line in soup.find_all('a'):
print("href =",line.attrs['href'])
輸出:
href = /wiki/anything/everything/nothing
href = /wiki/hello/how-about-now/nothing
添加回答
舉報(bào)