如何使用python +硒重命名列名

时间:2018-09-04 09:03:50

标签: python python-3.x selenium

我正在输出:

0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25
2 See Dimension 2   1102 sqft   653 sqft    40% Eco Winds 2 Discussions on forum    Under Construction  Mar, '19    New Property    "₹ 1.26 Cr ₹ 6,30,550 Approx. Registration Charges ₹ 5,000 Monthly
See Other Charges"  Bhandup (W), Mumbai, Bhandup West, Mumbai - Central Line, Maharashtra What's Nearby Bhandup East    Garden/Park, Main Road  Granite, Vitrified  1 Covered   24 Hours Available  No/Rare Powercut    2   2   Unfurnished Municipal Corporation of Greater Mumbai Freehold    Brokers please do not contact       "Visitor Parking
Waste Disposal
Rain Water Harvesting
Water Storage
Kids Play Area
Library And Business Centre
View all Amenities"
0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17                              
3 See Dimension 4   1276 sqft   Beaumonte 6 Discussions on forum    Under Construction  Dec, '23    New Property    1 Covered   ₹ 4.66 Cr Sion, Mumbai - Central Line, Maharashtra What's Nearby  Vitrified   24 Hours Available  No/Rare Powercut    5   Semi-Furnished  30      "Cafeteria/Food Court
Multipurpose Hall
Jogging and Strolling Track
Bar/Lounge
Cycling & Jogging Track
Gymnasium
View all Amenities"                             
0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15                                      
3   3   1104 sqft   Ideal   Ready to Move   New Property    None    Semi-Furnished  ₹ 5.73 Cr Juhu, Mumbai - Western Suburbs, Maharashtra What's Nearby   P51800001084 (The project has been registered via MahaRERA registration number: P51800001084 and is available on the website https://maharera.mahaonline.gov.in under registered projects.) 24 Hours Available  No/Rare Powercut    2   15  "Swimming Pool
Gymnasium"      

作为csv文件。

我想重命名所有列,但问题是当所有列的名称未按顺序排列时如何重命名所有列。

在所有列的第一行中可用,但在仅15列的第二行中可用。

如何匹配每个列名称,以使数据按定义的名称通过。

我尝试过:

for i in range(0, 10):
# Get unique links.
    for link in list_links:
        if "https://www.magicbricks.com/propertyDetails/" in link.get_attribute("href"):
            if link not in my_needed_links:
                my_needed_links.append(link)
                print (my_needed_links)

    # Go through of them and click on each.
    for unique_link in my_needed_links:

        try:
            unique_link.click()
            # Switch to the tab.
            driver.switch_to_window(driver.window_handles[1])
            # Do some stuff inside tab.
            # Close the tab.
            time.sleep(10)

            def get_elements_by_xpath(driver, xpath):
                        return [entry.text for entry in driver.find_elements_by_xpath(xpath)]

            search_entries = [
            ("Bedrooms", "//div[@class='seeBedRoomDimen']"),
            ("Bathrooms", "//div[@class='p_value']"),
            ("Super area", "//span[@id='coveredAreaDisplay']"),
            ("Floor", "//div[@class='p_value truncated']"),
            ("Lift", "//div[@class='p_value']")]

            #with open('textfile.csv', 'a+') as f_output:
            entries = []
            for name, xpath in search_entries:
                entries.append(get_elements_by_xpath(driver, xpath))
            data = [entry for entry in entries if len(entry)>5]
            df = pd.DataFrame(data)
            df.drop_duplicates(inplace=True)

            print (df)
            df.to_csv('nameoffile_1.csv', mode='a',index=False,encoding='utf-8')
            #df.to_csv('nameoffile.csv',mode='a', index=False,encoding='utf-8')

            get_elements_by_xpath(driver, xpath)
            time.sleep(2)

            driver.close()
            # Switch back to the main tab/window.
            driver.switch_to_window(driver.window_handles[0])

        except:

            pass

driver.find_element_by_link_text('Next Page').click()

0 个答案:

没有答案