Python在各行上打印单个锚文本和href
我有以下Python脚本,可以从页面中删除所有锚文本和href值:Python在各行上打印单个锚文本和href,python,Python,我有以下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(ur
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
这可能吗
谢谢只需将两个循环组合起来:
查找汤中的链接。查找所有('a'):
打印(“%s-%s”%”(link.text,link.get('href'))
谢谢。为了让它正常工作,我对它做了一些修改-print(link.text,“-”,link.get('href'))