简体   繁体   English

Python 抓取 Web 页

[英]Python Scraping Web page

I tried to Scrap all the 'a' tag placeholder values from this sample link: https://www.fundoodata.com/companies-in/list-of-apparel-stores-companies-in-india-i239我试图从这个示例链接中删除所有“a”标签占位符值: https://www.fundoodata.com/companies-in/list-of-apparel-stores-companies-in-india-i239

What I need is I want to copy only the names of 'a' tag and need to save it to a csv file我需要的是我只想复制“a”标签的名称,并需要将其保存到 csv 文件中

I'm beginner can anyone help me out below is my wrong code:我是初学者,任何人都可以帮助我下面是我的错误代码:

# importing the modules
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
import time
import os


link = "https://www.fundoodata.com/companies-in/list-of-apparel-stores-companies-in-india-i239"




# instantiating empty lists
nameList = []


for i in range(1) :
    driver = webdriver.Chrome(ChromeDriverManager().install())

 
    # fetching all the store details
    storeDetails = driver.find_elements_by_class_name('search-result')
 

 
    # iterating the storeDetails
    for j in range(len(storeDetails)):
         
        # fetching the name, address and contact for each entry
        name = storeDetails[j].find_element_by_class_name('heading').text
    
         
        myList = []
         
        nameList.append(name)


    driver.close()
 
     
# initialize data of lists.
data = {'Company Name': nameList,}
 
# Create DataFrame
df = pd.DataFrame(data)
print(df)
 
# Save Data as .csv
df.to_csv("D:\xxx\xxx\xx\xxx\demo.csv", mode='w+', header = False)

There are lots of problems here first of which:这里有很多问题,首先是:

## to open webpage
driver.get(link)

secondly you don't need the first for loop at all.其次,您根本不需要第一个 for 循环。 Lastly:最后:

from selenium.webdriver.common.by import By
## find the a tags inside the search results
storedetails = driver.find_elements(By.CSS_SELECTOR, 'div.heading a')
## iterating over elements list
for name in storedetails:
    ## appending the a tag text
    nameList.append(name.text)

I hope this helped: :)我希望这会有所帮助::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM