1 回答

TA贡献1788条经验 获得超4个赞
OP:开头必须是 href="/wiki/ 中间可以是任何内容,结尾必须是 "
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,我会选择 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
添加回答
举报