【发布时间】:2021-08-28 13:13:03
【问题描述】:
import json
from io import StringIO
from bs4 import BeautifulSoup
from requests_html import HTMLSession
import time
from selenium import webdriver
import requests
import pandas as pd
import numpy as np
from selenium.webdriver.chrome.options import Options
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset = False)
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome_options = Options()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(PATH, options = chrome_options)
# driver.minimize_window()
for b in range(3):
url = "https://www.healthgrades.com/usearch?what=Marriage%20%26%20Family%20Therapy&entityCode=PS303&where=CA&pageNum={}&sort.provider=bestmatch&state=CA".format(b+1)
driver.implicitly_wait(50)
driver.get(url)
wait = WebDriverWait(driver, 500)
button = driver.find_element_by_xpath("//a[@class = 'provider-name__lnk']")
length = len(driver.find_elements(By.CSS_SELECTOR, "a[data-qa-target='provider-details-provider-name']"))
j = 0
for i in range(length):
elements = driver.find_elements(By.XPATH, "//a[@data-qa-target='provider-details-provider-name']")
print(elements[j].text)
# ActionChains(driver).move_to_element(elements[j].find_element_by_xpath(".//ancestor::div[contains(@class,'card-summary')]/following-sibling::div[contains(@class,'card-cta')]/a")).click().perform()
button.click()
time.sleep(2)
phone = driver.find_element_by_xpath("//a[@class='toggle-phone-number-button']")
print(phone.text)
# print(wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@class='click-to-call-button-secondary hg-track mobile-click-to-call']"))).text)
driver.execute_script("window.history.go(-1)")
time.sleep(2)
j = j + 1
大家好,这个平台上的某个人已经帮助了我这段代码,但我扩展了它。它应该做什么:刮掉治疗师的名字,点击他们的链接,提取电话号码,回到第一个网站,重复。它的作用:抓取名称,点击链接。它找不到电话号码。我可能使用了错误的 ID,因为它返回错误“非类型没有属性文本”。谁能帮我修复第 19 行中的代码,以便打印电话号码?非常感谢任何帮助。
【问题讨论】:
-
请贴出完整代码。 (包括进口)
-
@Ram 我发布了我使用的导入