Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在各行上打印单个锚文本和href_Python - Fatal编程技术网

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

我有以下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
这可能吗


谢谢

只需将两个循环组合起来:

查找汤中的链接。查找所有('a'):
打印(“%s-%s”%”(link.text,link.get('href'))

谢谢。为了让它正常工作,我对它做了一些修改-print(link.text,“-”,link.get('href'))