我正试图从这个网站上获取过滤结果.

首先,我应用了过滤器"Código,descripión o referencea del phamplite",然后创建了一个新容器,并 Select 了选项"Contiene"&最后,我搜索了一个特定的词(在本例中是"anestesia"),但我不知道如何从所有过滤结果中刮取结果表,以获得"descripionón del implicate"部分中出现的链接.

这是我的代码:

import random
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import requests
from lxml import html


s=Service('./chromedriver.exe')
driver = webdriver.Chrome(service=s)

driver.get('https://compranet.hacienda.gob.mx/esop/guest/go/public/opportunity/current? 
locale=es_MX')
sleep(5)
driver.find_element(By.XPATH ,"//*[@id='widget_filterPickerSelect']/div[1]/input").click()
sleep(5)
driver.find_element(By.XPATH,"//*[@id='filterPickerSelect_popup1']").click()
sleep(5)
driver.find_element(By.XPATH,"//*[@id='projectInfo_FILTER_OPERATOR_ID']/option[2]").click()
sleep(5)
busqueda = driver.find_element(By.XPATH,"//*[@id='projectInfo_FILTER']")
busqueda.send_keys("anestesia")
busqueda.send_keys(Keys.ENTER)

特别是这是我想要刮的

<a href="#fh" class="detailLink" onclick="javascript:goToDetail('2110224', '01000');stopEventPropagation(event);" title="Ver detalle: PC-050GYR017-E140-2022    SERVICIO INTEGRAL DE ANESTESIA, PARA EL EJERCICIO  DEL 1º">PC-050GYR017-E140-2022   SERVICIO INTEGRAL DE ANESTESIA, PARA EL EJERCICIO  DEL 1º</a>

我需要找到链接.

推荐答案

您需要使用显式等待.

为了获得最终页面上的链接,您应该使用find_elementsvisibility_of_all_elements_located,因为存在多个web元素.如果你只是想删除链接,我建议你只使用第print(link.get_attribute('href'))行,其余两行你可以 comments .

Code:

s=Service('./chromedriver.exe')
driver = webdriver.Chrome(service=s)

driver.maximize_window()
wait = WebDriverWait(driver, 20)

driver.get('https://compranet.hacienda.gob.mx/esop/guest/go/public/opportunity/current?locale=es_MX')

wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='▼ ']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='filterPickerSelect_popup1']"))).click()

select = Select(wait.until(EC.presence_of_element_located((By.ID, "projectInfo_FILTER_OPERATOR_ID"))))
select.select_by_value('CONTAINS')

busqueda = wait.until(EC.visibility_of_element_located((By.ID, "projectInfo_FILTER")))
busqueda.send_keys("anestesia")
time.sleep(2)
busqueda.send_keys(Keys.ENTER)

links = wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='detailLink'][@href]")))
for link in links:
    print(link.get_attribute('innerText'))
    print(link.get_attribute('href'))
    print(link.get_attribute('title'))

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Output:

PC-050GYR017-E140-2022 SERVICIO INTEGRAL DE ANESTESIA, PARA EL EJERCICIO DEL 1º
https://compranet.hacienda.gob.mx/esop/toolkit/opportunity/current/list.si?reset=true&resetstored=true&userAct=changeLangIndex&language=es_MX&_ncp=1649225706261.4394-1#fh
Ver detalle: PC-050GYR017-E140-2022 SERVICIO INTEGRAL DE ANESTESIA, PARA EL EJERCICIO  DEL 1º
SERVICIO DE MANTENIMIENTO PREVENTIVO Y CORRECTIVO DE EQUIPO MÉDICO
https://compranet.hacienda.gob.mx/esop/toolkit/opportunity/current/list.si?reset=true&resetstored=true&userAct=changeLangIndex&language=es_MX&_ncp=1649225706261.4394-1#fh
Ver detalle: SERVICIO DE MANTENIMIENTO PREVENTIVO Y CORRECTIVO DE EQUIPO MÉDICO

Python相关问答推荐

为什么判断pd.DataFrame的值与判断pd.Series的值存在差异(如果索引中有值)?

Python:根据创建时间合并两个收件箱

在编写要Excel的数据透视框架时修复标题行

使用多个性能指标执行循环特征消除

从包含数字和单词的文件中读取和获取数据集

即使在可见的情况下也不相互作用

当多个值具有相同模式时返回空

scikit-learn导入无法导入名称METRIC_MAPPING64'

为什么这个带有List输入的简单numba函数这么慢

优化pytorch函数以消除for循环

NP.round解算数据后NP.unique

Telethon加入私有频道

如何在WSL2中更新Python到最新版本(3.12.2)?

连接一个rabrame和另一个1d rabrame不是问题,但当使用[...]'运算符会产生不同的结果

Scrapy和Great Expectations(great_expectations)—不合作

未知依赖项pin—1阻止conda安装""

如何从列表框中 Select 而不出错?

解决调用嵌入式函数的XSLT中表达式的语法移位/归约冲突

Flash只从html表单中获取一个值

Pandas:计算中间时间条目的总时间增量