[英]How to parse several attributes of website with same class name in python?
在搜索栏中搜索 CAP 并输入结果后,我想用 Pyhthon 解析来自该网站( https://www.conad.it/ )的地址。 对于许多 CAP 来说,有很多商店的地址会产生结果,我想刮掉所有的地址,而不仅仅是第一个(这是我的代码现在正在做的事情)。
到目前为止,这是我的代码:
driver = webdriver.Chrome('pathtoChrome/chromedriver.exe')
driver.get("https://www.conad.it/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='javascript:void(0)']"))).click() # accept the cookies
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='location-input']"))).send_keys("11100")
driver.find_element_by_xpath("//input[@class = 'btn btn-default btn-lg btn-block']").click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'col-md-8')]"))).get_attribute("innerHTML"))
这是最终的output:
<h3>Conad</h3><p>Frazione Condemine 84, 11010 Sarre</p><div class="extra-services extra-services-buttons extra-services-desktop extra-services-simple"><ul class="carousel-services"></ul></div>
我只想要 output 中的<p>
中的 output 但对于 class 'col-md-8
地址中的所有属性,对于第二个示例也是如此。
最理想的情况是,我想将它存储在一个数据集中,我可以在不同 CAP 的几个循环中使用 append,所以像这样(这还行不通..):
driver = webdriver.Chrome('pathtoChrome/chromedriver.exe')
driver.get("https://www.conad.it/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@href='javascript:void(0)']"))).click() # accept the cookies
CAPS = ['11100']
for CAP in CAPS:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='location-input']"))).send_keys(CAP)
driver.find_element_by_xpath("//input[@class = 'btn btn-default btn-lg btn-block']").click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class,'col-md-8')]"))).get_attribute("innerHTML"))
任何帮助表示赞赏!
您可以使用WebDriverWait()
并等待visibility_of_all_elements_located
() 并遵循xpath
选项来获取列表中的所有 p 标记值。
print([item.text for item in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@class,'col-md-8')]//p")))])
您的 output 就像一个列表。
['Frazione Condemine 84, 11010 Sarre', 'Grand Chemin C/c Centreville 3, 11020 Saint-christophe', "Localita' Arensod 27, 11010 Sarre"]
你非常接近。 几点:
By.XPATH, "//a[@href='javascript:void(0)']
不是一个稳定的定位器策略。相反你可以使用更稳定和优化By.XPATH, "//a[@class='cc_btn cc_btn_accept_all']
如下:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cc_btn cc_btn_accept_all']"))).click()
Frazione Condemine 84, 11010 Sarre
等文本位于<div>
的子<p>
标记内,因此您需要更深入一点。
同样,不是By.XPATH, "//div[contains(@class,'col-md-8')]"
因为<p>
标记后面总是跟着包含文本Conad的<h3>
我们可以构造一个更可靠的定位器。
优化的解决方案:
使用xpath和get_attribute("innerHTML")
:
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//h3[contains(.,'Conad')]//following-sibling::p[1]")))])
使用xpath和文本属性:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//h3[contains(.,'Conad')]//following-sibling::p[1]")))])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.