Python获取锚文本链接和href值,但忽略图像链接

2024-09-28 17:15:37 发布

您现在位置: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/contact-us"

session = HTMLSession()
r = session.get(url)

b  = requests.get(url)
soup = BeautifulSoup(b.text, "lxml")

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

它工作正常,但它也会刮取图像链接,如果是图像,则输出“-”。例如:

Contact Us - /contact-us
About Us - /about
- /locations

我希望它忽略任何图像href链接,因此输出为:

Contact Us - /contact-us
About Us - /about

这可能吗

谢谢


Tags: from图像importurlget链接sessionlink