打印两个特定的其他字母或字符之间的文本,我不知道是否应该使用 urllib
print text that's between two specific other letters or characters, I don't know if urllib should be used
<li><a href="/alumni/">Alumni & Friends</a></li>
如何从上面的 HTML 代码中只打印 '/alumni' 和 "Alumni & Friends"?
"Since 2004, it's been saving programmers hours or days of work on quick-turnaround screen scraping projects."
使用 BeautifulSoup 非常简单:
html_doc = """<li><a href="/alumni/">Alumni & Friends</a></li>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))
# or if you want exactly '/alumni' use
# print(link.get('href')[:-1])
print(link.text)
输出:
/alumni/
Alumni & Friends
您可以通过以下方式安装 BeautifulSoup:
pip install beautifulsoup4
或参阅 Installing Beautiful Soup 了解更多安装选项。
<li><a href="/alumni/">Alumni & Friends</a></li>
如何从上面的 HTML 代码中只打印 '/alumni' 和 "Alumni & Friends"?
"Since 2004, it's been saving programmers hours or days of work on quick-turnaround screen scraping projects."
使用 BeautifulSoup 非常简单:
html_doc = """<li><a href="/alumni/">Alumni & Friends</a></li>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))
# or if you want exactly '/alumni' use
# print(link.get('href')[:-1])
print(link.text)
输出:
/alumni/
Alumni & Friends
您可以通过以下方式安装 BeautifulSoup:
pip install beautifulsoup4
或参阅 Installing Beautiful Soup 了解更多安装选项。