Ошибка при объединении данных для нескольких страниц в Python - PullRequest
1 голос
/ 06 июня 2019

Я сталкиваюсь с ошибкой при объединении данных на нескольких страницах и их экспорте в один файл CSV. Согласно моему коду, данные экспортируются на страницу 10, но после страницы № 10 они работают.

import urllib.request
from bs4 import BeautifulSoup
import csv
import os
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
import time
import pandas as pd
import os

url = 'http://www.igrmaharashtra.gov.in/eASR/eASRCommon.aspx? 
hDistName=Buldhana'
chrome_path = 

r'C: /Users/User/AppData/Local/Programs/Python/Python36/Scripts/chromedriver.exe'

d = webdriver.Chrome(executable_path=chrome_path)
d.implicitly_wait(10)
d.get(url)

Выберите (d.find_element_by_name ( 'ctl00 $ ContentPlaceHolder5 $ ddlTaluka')). Select_by_value ( '7') Выберите (d.find_element_by_name ( 'ctl00 $ ContentPlaceHolder5 $ ddlVillage')). Select_by_value ( '1464')

tableElement = d.find_element_by_id(
    'ctl00_ContentPlaceHolder5_grdUrbanSubZoneWiseRate')
table = pd.read_html(tableElement.get_attribute('outerHTML'))[0]
#print(table)
table.columns = table.iloc[0]
table = table.iloc[1:]
#print(type(table))
table = table[table.Select == 'SurveyNo']
#print(table) #assumption SurveyNo exists for all wanted rows
surveyNo_scripts = [item.get_attribute('href') for item in 

d.find_elements_by_css_selector ( "# ctl00_ContentPlaceHolder5_grdUrbanSubZoneWi seRate [href * = 'Select $'] ")]

#print(surveyNo_scripts)
i = 0
for script in surveyNo_scripts:
    d.execute_script(script)
    surveys = d.find_element_by_css_selector('textarea').text
    table.iloc[[i],table.columns.get_loc('Select')] = surveys
    i += 1

print(table)

j=2
while True:
if len(d.find_elements_by_css_selector("#ctl00_ContentPlaceHolder5_grdUrbanSubZoneWiseRate a[href*='Page${}']".format(j)))>0:

 #print( d.find_elements_by_css_selector("#ctl00_ContentPlaceHolder5_grdUrbanSubZoneWiseRate a[href*='Page${}']".format(i))[0].get_attribute('href'))
    d.find_elements_by_css_selector("#ctl00_ContentPlaceHolder5_grdUrbanSubZoneWiseRate a[href*='Page${}']".format(j))[0].click()
    tableElement = d.find_element_by_css_selector( 
    "#ctl00_ContentPlaceHolder5_grdUrbanSubZoneWiseRate")
    table1 = pd.read_html(tableElement.get_attribute('outerHTML'))[0]
    table1.columns = table1.iloc[0]
    table1 = table1.iloc[1:]
    #print(type(table))
    table1 = table1[table1.Select == 'SurveyNo']
    #print(table) #assumption SurveyNo exists for all wanted rows
    surveyNo_scripts = [item.get_attribute('href') for item in 
    d.find_elements_by_css_selector(
                "#ctl00_ContentPlaceHolder5_grdUrbanSubZoneWiseRate 
    [href*='Select$']")]
    #print(surveyNo_scripts)
    i = 0
    for script in surveyNo_scripts:
                    d.execute_script(script)
                    surveys = 
                    d.find_element_by_css_selector('textarea').text
                    table1.iloc[[i],table1.columns.get_loc('Select')] = 
                    surveys
                    i += 1

    #print(table1)
    #table = table.append(table1.reindex(columns=table.columns))
    table1.columns = table.columns
    table = pd.concat([table, table1] ,ignore_index=True)
    print(table)
    j+=1
else:
    break
table.to_csv(r"C:\Users\Guest\Desktop\Sample_buldhana.csv", sep=',', encoding='utf-8-sig',index = False )    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...