Python在单独的行上打印单独的锚文本和href



我有以下Python脚本,可以从页面中抓取所有锚文本和href值:

from requests_html import HTMLSession
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
url="https://www.mydomain.co.uk/path-here"
session = HTMLSession()
r = session.get(url)
b  = requests.get(url)
soup = BeautifulSoup(b.text, "lxml")
for link in soup.find_all('a'):
print(link.get('href'))
for tag in soup.find_all('a'):
print (tag.text)

它工作正常,但我希望它在同一行打印锚文本(加上短划线(和相应的href值,例如:

get quote - https://www.mydomain.co.uk/get-quote
contact us - https://www.mydomain.co.uk/contact us

这可能吗?

感谢

只需组合两个循环:

for link in soup.find_all('a'):
print("%s - %s" % (link.text, link.get('href'))

相关内容

最新更新