我有一个输入csv,我想输出csv。输入生成一些url,我想将它们附加到现有的datafram中

2024-06-03 16:40:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个CSV文件和一些关键字。使用关键字,我使用serpapi生成10-15个url。我想将URL附加到与该关键字相关的列URL。对于一个关键字,有多个URL。如果我不能添加网址到各自的关键字关键字关键字将与不同的网址复制。你知道吗

你知道吗测试.csv文件

keyword            value         cost
electrician         15            3.6
plumber             12            5.9

我想要这样的结果输出.csv文件

keyword        value     cost         url
electrician     15       3.6        www.ex1.com
electrician     15       3.6        www.ex2.com
electrician     15       3.6        www.ex3.com
.
.
.
.
plumber         12       5.9         www.ee.com
plumber         12       5.9         www.ee2.com
.
.

是的。你知道吗

这是我正在使用的代码;我用pandas尝试过,但它只附加了最后一个URL。你知道吗

df = pd.read_csv("test.csv")                                                                                  
col = df['keywords']                                                                                          
for k in col:                                                                                                 
    print(k)                                                                                                  
    client_params = {                                                                                         
      "q": k,                                                                                                 
      "google_domain": "google.co.uk",                                                                        
      "location": "United+Kingdom",                                                                           
      "hl": "en",                                                                                             
      "gl": "uk",                                                                                             
      "num": "15",                                                                                            
      "serp_api_key": "********************",                     
    }                                                                                                         
    client = GoogleSearchResults(client_params)                                                               
    json_results = client.get_json()                                                                          
    #print(json_results)                                                                                      
    #print(type(json_results))                                                                                
    results = pd.DataFrame()                                                                                  
    print(results)                                                                                            
    for title in json_results['organic_results']:                                                             
        print(title['link'])                                                                                  
        df['URL'] = title['link']                                                                             
        results = results.append(df).reset_index(drop = True)                                                 
    # df.to_csv('output.csv',index=False, header= False)                                                      
results.to_csv('output.csv',index=False)  

但我得到的结果是:

keyword       value    cost       urls
electrician    15      3.6      www.ex.xom
plumber        12      5.9      www.ex.xom

Tags: 文件csvcomclientjsonurldfvalue
1条回答
网友
1楼 · 发布于 2024-06-03 16:40:50

这是因为循环中有results。每次迭代之后,它都会覆盖上一次,从而将最后一次写入作为最后一次迭代。您需要在进入循环之前初始化它,然后才能继续附加到它。比如:

注意:您可能不想包含api密钥,所以我从您的答案中删除了它。您需要输入api\ U密钥

import pandas as pd
from lib.google_search_results import GoogleSearchResults

df = pd.read_csv("test.csv")                                                                  

results = pd.DataFrame()   
for idx, row in df.iterrows():
    k = row['keyword']  
    value = row['value']
    cost = row['cost']                                                                            
    client_params = {                                                                         
      "q": k,                                                                                 
      "google_domain": "google.co.uk",                                                        
      "location": "United+Kingdom",                                                           
      "hl": "en",                                                                             
      "gl": "uk",                                                                             
      "num": "15",                                                                            
      "serp_api_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",     
    }                                                                                         
    client = GoogleSearchResults(client_params)                                               
    json_results = client.get_json()                                                          

    for title in json_results['organic_results']:                                             
        print(title['link'])                                                                  
        title =  title['link']

        temp_df = pd.DataFrame([[k, value, cost,  title]], columns=['keyword', 'value','cost','URL'])
        results = results.append(temp_df).reset_index(drop = True)                                 

results.to_csv('output.csv',index=False)  

相关问题 更多 >