我刚开始使用python,遇到了一个问题。我正试图创建一个网络抓取的循环来获取信息,但我的循环需要点击两次。我正在使用的页面是:https://www.becual.com/oportunidades并且对于每个反转机会,我需要点击然后获得信息";credito soliciado";以及";empresa pagadora";。这是我用于循环的代码。
firmas = []
for i in range(0, size):
detalles = browser.find_elements_by_xpath('//div[@class="col-xs-12 col-sm-3 col-md-3 imagen-oportunidad"]')
detalles[i].click()
time.sleep(3)
monto = browser.find_element_by_css_selector('span:nth-last-child(3)').text
garantia = browser.find_element_by_xpath('/html/body/app-root/div[2]/becual-opportunity-detail/div/div[2]/div/div[3]/div/div[1]/ul/li[3]/a')
garantia.click()
time.sleep(3)
empresa = browser.find_element_by_xpath("/html/body/app-root/div[2]/becual-opportunity-detail/div/div[2]/div/div[3]/div/div[2]/div[3]/div/div[2]/div[2]/strong").text
browser.back()
time.sleep(10)
firmas.append({'Monto': monto, 'Empresa': empresa})
我的循环一直工作到3号。但是它说它是不可点击的。我只点击第三个元素,点击使用mi循环代码中编写的xpath("//div[@class="col-xs-12 col-sm-3 col-md-3 imagen oportunidad"]"(。我不明白为什么循环不起作用,但单独做却起作用。
您可以使用requests
从外部API加载数据:
import json
import requests
from bs4 import BeautifulSoup
url = ' https://api.becual.com/api/oportunidad?incluirHistorico=false&page=1&limit=10000'
data = requests.get(url).json()
# uncomment to print all data:
# print(json.dumps(data, indent=4))
for data in data['data']:
o = requests.get('https://api.becual.com/api/oportunidad/' + data['idOportunidad']).json()
# uncomment to print all data about opportunity:
# print(json.dumps(o, indent=4))
nombre = o['garantias'][0]['nombreEmpresa'] if o['garantias'] else '-'
print('{:<15} {:<15} {}'.format(o['idOportunidad'], o['montoCredito'], nombre))
打印:
CR1808213969 16000000 Altamira Chile
CR1712292810 10400000 Jej Ingeniería
CR1712202756 4800000 Soc. Agricola Requingua
CR1712052601 15583739 Soc. Agricola Requingua
CR1801042850 7200000 International Paper Cartones
CR1809144426 707264 CONSTRUCTORA METROPOLITANA S.A.
CR1710102345 3500000 Soc. Agricola Requingua
CR1712262781 8700000 Compañía Industrial el Volcán
CR1711072497 4200000 Viña Santa Ema
CR1709272302 8100000 Soc. Agricola Requingua
CR1712272789 10400000 Jej Ingeniería
CR1802023015 11100000 SGS Chile
CR1801182911 5500000 Soc. Agricola Requingua
CR1711022486 6100000 International Paper Cartones
CR1711102516 8723983 International Paper Cartones
...and so on.
元素未在DOM中正确加载,也未处于接受点击的状态。尝试以下代码,等待元素可点击
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//div[@class="col-xs-12 col-sm-3 col-md-3 imagen-oportunidad"]')))
element.click();