Python 如何从搜索页面获取动态数据(如果可用)并将其保存到与搜索结果相关的csv

Python 如何从搜索页面获取动态数据(如果可用)并将其保存到与搜索结果相关的csv,python,csv,selenium-webdriver,Python,Csv,Selenium Webdriver,我被一个情景困住了 我有这样的情况 例如,让我们考虑谷歌或LinkedIn搜索。假设每页获取10个结果。让我们假设在每个结果中几乎没有数据,例如标题链接(第一行),页面链接(第二行)将出现在所有配置文件中。第三行和第四行将有一些描述,这仅适用于少数概要文件。我想自动化并保存每个结果的可用数据。例如,标题链接、页面链接、说明(如果可用)并将其附加到CSV文件中 预期的格式如下所示。 名称|位置| URL |说明 X1 | X2 | X3 | Y4(如有) Y1 | Y2 | Y3 |空(如果不存在

我被一个情景困住了

我有这样的情况

例如,让我们考虑谷歌或LinkedIn搜索。假设每页获取10个结果。让我们假设在每个结果中几乎没有数据,例如标题链接(第一行),页面链接(第二行)将出现在所有配置文件中。第三行和第四行将有一些描述,这仅适用于少数概要文件。我想自动化并保存每个结果的可用数据。例如,标题链接、页面链接、说明(如果可用)并将其附加到CSV文件中

预期的格式如下所示。 名称|位置| URL |说明

X1 | X2 | X3 | Y4(如有)

Y1 | Y2 | Y3 |空(如果不存在)

代码如下:

 while True:
        wait = WebDriverWait(driver, 150)
        profile_loc = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".demographic>dd>bdi")))
        profile_name = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".title.main-headline")))
        profile_all = wait.until(EC.presence_of_all_elements_located((By.XPATH,".//*[contains(@class,'mod result idx')]")))

        profile_current = wait.until(EC.presence_of_all_elements_located((By.XPATH,"//*[@class='label' and text()='Current']//following::dd[1]/p[1]")))


        for each_link in profile_links:
            page_links = each_link.get_attribute('href')
            print(page_links)

        for each_name in profile_name:
            page_name = each_name.text
            print(page_name)

        for each_loc in profile_loc:
            page_loc = each_loc.text
            print(page_loc)

        for each_current in profile_current:
            page_current = str(each_current.text.encode("utf-8"))
            print(page_current)

            appendFile = open("E:\Balaji\Profile-Links.csv", 'a')
            appendFile.write(str(page_name + "|" + page_loc + "|" + page_links + "|" + page_current + "\n"))
            appendFile.close()
我这样做了,它为所有搜索结果(即10个)提供了链接和其他数据,并附加了这些数据。但我的问题是,我无法将描述部分(动态存在/不存在)附加到其各自的链接或概要文件中

我识别了描述(如果存在的话)并将其保存到一个变量中,比如(profile current)。我尝试了一些可笑的代码,如下面所示,但它会被任意附加

检查描述是否存在,并将结果保存到当前页面 将结果附加到csv文件中 请帮我解决这个问题

 for each_current in profile_current:
if profile_current != "":
    page_current = str(each_current.text.encode("utf-8"))
else:
    page_current = "Empty"
print page_current
  appendFile = open("E:\Balaji\Profile-Links.csv", 'a')
            appendFile.write(str(page_name + "|" + page_loc + "|" + page_links + "|" + page_current + "\n"))
            appendFile.close()