【发布时间】:2019-05-31 19:50:45
【问题描述】:
当您单击每部手机下方的“查看选项”按钮并将它们放入字符串列表时,我正在尝试提取 https://shop.freedommobile.ca/devices 的所有网址。
我正在使用 python 和 Selenium 并等待库。 我已经尝试在我的参数中使用 .text。但是,我一直遇到一个错误,指出:
typeError: 'str' 对象不可调用 第 17 行是问题所在。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
class phoneCost:
driver.get("https://shop.freedommobile.ca/devices")
# extract the names of the phones
wait = WebDriverWait(driver, 20) #10 second wait
XPathLocation = """B//*[@id="skip-navigation"]/div/div/div[1]/div/div[2]/a'"""
phonePlanLinksRaw = wait.until(EC.presence_of_all_elements_located(By.XPATH(XPathLocation)))
phonePlanLinks = []
for element in range(len(phonePlanLinksRaw)):
link = element
phonePlanLinks.append(str(link))
numLink = 1
for element in range(len(phonePlanLinks)):
print("phone " + str(numLink) + " : " + phonePlanLinks[element])
numLink += 1
应该返回一个字符串格式的url列表:
,
https://shop.freedommobile.ca/devices/Apple/iPhone_XS?sku=190198790569&planSku=Freedom%20Big%20Gig%
,
任何帮助表示赞赏 谢谢
【问题讨论】:
-
链接是一个webelement...首先获取href。不太确定这是否是您的问题。
标签: python selenium web-scraping scrapy