Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用selenium拾取数据_Python_Selenium_Web Scraping - Fatal编程技术网

Python 使用selenium拾取数据

Python 使用selenium拾取数据,python,selenium,web-scraping,Python,Selenium,Web Scraping,我无法使用selenium webdriver获取数据。Im获取错误代码和文本(标题) 我的代码: from selenium import webdriver from selenium.webdriver.common.keys import Keys import time from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from sel

我无法使用selenium webdriver获取数据。Im获取错误代码和文本(标题)

我的代码:

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

PATH =".\msedgedriver.exe"
driver = webdriver.Edge(PATH)

driver.get("https://www.verivox.de/stromvergleich/vergleich/#/?plz=10555&persons=on&usage=3500&bonus=OnlyCompliant&profile=H0&product=electricity&source=1&q=WzYsMCwxLDEsMSwxLDEsMiwyMCwwLDEsNzQxMTIyLCI3MTA4NSIsMSwyNDAsMjQwLDM1MDAsMCwwLDAsOTk5LC0xLC0xLC0xLDAsMCwiVG90YWxDb3N0cyIsIkFzY2VuZGluZyIsIk5vbmUiLDM4LCJBbm51YWxDb3N0VmlldyIsMF0%3D&partnerid=1")
print(driver.title)

try:
    main = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "nrg-main-container"))
    )
    
    articles = main.find_elements_by_tag_name("li")
    for article in articles:
        header = article.find_element_by_class_name("result-item")
        print(header.text)



finally:
    driver.quit()
我想从类名结果项上设置的前20个标题信息中提取数据。图片吼叫

使用
WebDriverWait()
并等待位于
()的所有元素的
可见性以及以下css选择器

allheader=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"li[class='result-item'] .result-name-area>.result-name")))
for header in allheader:
     print("Header: " + header.text)
控制台输出:

Header: Easy24 Strom
Header: Vollstrom sicher
Header: Natur24 Strom
Header: eprimoStrom 24
Header: E.ON Strom Extra
Header: Strom Zuhause+(24
Header: Super Schlau Fuxx
Header: Q.ENERGY Eco24 Plus
Header: MeinSmartTarif Strom
Header: Doppel Plus
Header: innogy direkt Strom
Header: ÖkoStrom Relax 24
Header: E.ON Strom 24
Header: Easy12 Extra Strom
Header: innogy direktStrom12
Header: Spar Smart Sofort 12
Header: E.ON Strom Extra
Header: eprimo PrimaKlima
Header: Lidl-Strom Extra
Header: R(H)EINPOWER Strom

使用python请求废弃数据?谢谢,你帮了我很多忙,我有一个问题,如果我需要搜索多个数据,我怎么做?在同一排perfect@Kalakutas:您需要更改逻辑。如果您能发布一个新问题并给出预期结果,我将不胜感激。
Header: Easy24 Strom
Header: Vollstrom sicher
Header: Natur24 Strom
Header: eprimoStrom 24
Header: E.ON Strom Extra
Header: Strom Zuhause+(24
Header: Super Schlau Fuxx
Header: Q.ENERGY Eco24 Plus
Header: MeinSmartTarif Strom
Header: Doppel Plus
Header: innogy direkt Strom
Header: ÖkoStrom Relax 24
Header: E.ON Strom 24
Header: Easy12 Extra Strom
Header: innogy direktStrom12
Header: Spar Smart Sofort 12
Header: E.ON Strom Extra
Header: eprimo PrimaKlima
Header: Lidl-Strom Extra
Header: R(H)EINPOWER Strom