簡體   English   中英

Python 抓取 Web 頁

[英]Python Scraping Web page

我試圖從這個示例鏈接中刪除所有“a”標簽占位符值: https://www.fundoodata.com/companies-in/list-of-apparel-stores-companies-in-india-i239

我需要的是我只想復制“a”標簽的名稱,並需要將其保存到 csv 文件中

我是初學者,任何人都可以幫助我下面是我的錯誤代碼:

# 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)

這里有很多問題,首先是:

## to open webpage
driver.get(link)

其次,您根本不需要第一個 for 循環。 最后:

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)

我希望這會有所幫助::)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM