繁体   English   中英

BeautifulSoup4 在链接中找到多个带有特定文本的 href 链接

[英]BeautifulSoup4 find multiple href's links with specific text in links

我正在尝试过滤所有带有字符串“3080”的 href 链接,我看到了一些示例,但我无法将它们应用到我的代码中。 有人可以告诉我如何只打印链接。

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
import driver_functions

gpu = '3080'
url = f'https://www.alternate.de/listing.xhtml?q={gpu}'

options = webdriver.ChromeOptions()
options.add_argument('--headless')

if __name__ == '__main__':
    browser = webdriver.Chrome(options=options, service=Service('chromedriver.exe'))
    try:

        browser.get(url)

        time.sleep(2)

        html = browser.page_source

        soup = BeautifulSoup(html, 'html.parser')

        gpu_list = soup.select("a", class_="grid-container listing")

        for link in gpu_list:
            print(link['href'])

        browser.quit()
    except:
        driver_functions.browserstatus(browser)

输出

您可以使用带有 * contains 运算符的 css attribute = value css 选择器来定位包含该gpu变量的列表中的href s。 如果您发现要考虑的边缘情况,您显然可以开发此 css 选择器列表。 我只看了给定的网址。

gpu_links= [i['href'] for i in soup.select(f".listing [href*='{gpu}']")]

试试这个作为你的选择器gpu_list = soup.select('#lazyListingContainer > div > div > div.grid-container.listing > a')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM