无法使用硒从页面中每个名为“标题”的类中获取数据

尼哈·沙玛(Neha Sharma)

嗨,我是数据收集的新手。在这里,我试图从所有具有“标题”属性的类中删除数据但是在我的代码中,即使我使用for循环进行迭代,它也只打印第一个元素。

预期输出-从所有具有“标题”属性页面类中废弃数据

实际输出-仅从类名称为“ heading”的第一个元素中删除数据,甚至不单击next按钮。

我用于测试的网站在这里

from selenium import webdriver
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd
from openpyxl.workbook import Workbook


DRIVER_PATH = 'C:/Users/Aishwary/Downloads/chromedriver_win32/chromedriver'

driver = webdriver.Chrome(executable_path=DRIVER_PATH)

driver.get('https://www.fundoodata.com/citiesindustry/19/2/list-of-information-technology-(it)-companies-in-noida')

# get all classes which has heading as a class name 
company_names = driver.find_elements_by_class_name('heading')

# to store all companies names from heading class name
names_list = []

while True:

    try:
        for name in company_names: # iterate each name in all div classes named as heading
            text = name.text    # get text data from those elements
            names_list.append(text)
            print(text)
            # Click on next button to get data from next pages as well
            driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="main-container"]/div[2]/div[4]/div[2]/div[44]/div[1]/ul/li[7]/a'))))
            driver.find_element_by_xpath('//*[@id="main-container"]/div[2]/div[4]/div[2]/div[44]/div[1]/ul/li[7]/a').click()

    except (TimeoutException, WebDriverException) as e:
        print("Last page reached")
        break


driver.quit()

# Store those data in excel sheet
df = pd.DataFrame(names_list)
writer = pd.ExcelWriter('companies_names.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='List')
writer.save()
安德烈·凯斯利(Andrej Kesely)

该脚本将从页面中获取所有公司名称:

import requests
import pandas as pd
from bs4 import BeautifulSoup


url = 'https://www.fundoodata.com/citiesindustry/19/2/list-of-information-technology-(it)-companies-in-noida'

all_data = []
while True:
    print(url)

    soup = BeautifulSoup( requests.get(url).content, 'html.parser' )
    for h in soup.select('div.heading'):
        all_data.append({'Name' : h.text})
        print(h.text)

    next_page = soup.select_one('a:contains("Next")')
    if not next_page:
        break

    url = 'https://www.fundoodata.com' + next_page['href']

df = pd.DataFrame(all_data)
print(df)

df.to_csv('data.csv')

印刷品:

                              Name
0                   BirlaSoft Ltd
1             HCL Infosystems Ltd
2            HCL Technologies Ltd
3           NIIT Technologies Ltd
4          3Pillar Global Pvt Ltd
..                             ...
481  Innovaccer Analytics Pvt Ltd
482         Kratikal Tech Pvt Ltd
483          Sofocle Technologies
484    SquadRun Solutions Pvt Ltd
485   Zaptas Technologies Pvt Ltd

[486 rows x 1 columns]

并保存data.csv(来自LibreOffice的截图):

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用特定的span类python硒获取页面中的所有元素

来自分类Dev

如何使用硒循环浏览页面并从每个页面获取数据?

来自分类Dev

遍历页面中的父类,并使用python中的硒确定父类是否存在子类

来自分类Dev

无法在每个父div /类中获取子div ID

来自分类Dev

如何从硒python中的同一类中获取不同的数据?

来自分类Dev

使用Python中的Selenium Webdriver获取td类的标题

来自分类Dev

使用Python中的Selenium Webdriver获取td类的标题

来自分类Dev

即使使用硒中的javascript方法也无法更改标题的名称

来自分类Dev

从熊猫数据中获取标题

来自分类Dev

python硒从表中获取数据

来自分类Dev

BeautifulSoup将无法使用硒获取页面源

来自分类Dev

Zend在构造函数中获取页面标题

来自分类Dev

流畅-在链接中获取页面标题

来自分类Dev

在themeablebrowser Cordova / Phonegap中获取页面标题

来自分类Dev

在Wikipedia中获取当前页面标题

来自分类Dev

使用python修改硒中的div类

来自分类Dev

无法使用 Python WebScraping 从列表中获取所有标题

来自分类Dev

如何使用硒确认页面标题?

来自分类Dev

使用scrapy-selenium模块从多个JavaScript页面中抓取硒数据

来自分类Dev

使用Selenium Web Driver在Google搜索页面中捕获每个搜索结果的标题

来自分类Dev

硒-获取页面中的所有iframe(甚至是嵌套的)?

来自分类Dev

如何使用硒在iframe中获取docx?

来自分类Dev

无法使用 PHP 从 JSON 中获取数据

来自分类Dev

回送4-无法从使用@ inject.tag()注入的类数组中获取元数据

来自分类Dev

为什么我无法使用Jooq的into方法获取类中的数据库字段值?

来自分类Dev

为什么我在Python中获取数据帧的每个索引的列标题?

来自分类Dev

在AJAX页面中获取数据

来自分类Dev

在AJAX页面中获取数据

来自分类Dev

无法使用硒从网站中查找元素

Related 相关文章

  1. 1

    使用特定的span类python硒获取页面中的所有元素

  2. 2

    如何使用硒循环浏览页面并从每个页面获取数据?

  3. 3

    遍历页面中的父类,并使用python中的硒确定父类是否存在子类

  4. 4

    无法在每个父div /类中获取子div ID

  5. 5

    如何从硒python中的同一类中获取不同的数据?

  6. 6

    使用Python中的Selenium Webdriver获取td类的标题

  7. 7

    使用Python中的Selenium Webdriver获取td类的标题

  8. 8

    即使使用硒中的javascript方法也无法更改标题的名称

  9. 9

    从熊猫数据中获取标题

  10. 10

    python硒从表中获取数据

  11. 11

    BeautifulSoup将无法使用硒获取页面源

  12. 12

    Zend在构造函数中获取页面标题

  13. 13

    流畅-在链接中获取页面标题

  14. 14

    在themeablebrowser Cordova / Phonegap中获取页面标题

  15. 15

    在Wikipedia中获取当前页面标题

  16. 16

    使用python修改硒中的div类

  17. 17

    无法使用 Python WebScraping 从列表中获取所有标题

  18. 18

    如何使用硒确认页面标题?

  19. 19

    使用scrapy-selenium模块从多个JavaScript页面中抓取硒数据

  20. 20

    使用Selenium Web Driver在Google搜索页面中捕获每个搜索结果的标题

  21. 21

    硒-获取页面中的所有iframe(甚至是嵌套的)?

  22. 22

    如何使用硒在iframe中获取docx?

  23. 23

    无法使用 PHP 从 JSON 中获取数据

  24. 24

    回送4-无法从使用@ inject.tag()注入的类数组中获取元数据

  25. 25

    为什么我无法使用Jooq的into方法获取类中的数据库字段值?

  26. 26

    为什么我在Python中获取数据帧的每个索引的列标题?

  27. 27

    在AJAX页面中获取数据

  28. 28

    在AJAX页面中获取数据

  29. 29

    无法使用硒从网站中查找元素

热门标签

归档