在get url python中传递dataframe列vale作为参数

安基·库马尔·沙玛(Ankit Kumar Sharma)

我试图在循环中将列值作为url参数传递,我也试图将结果放入json文件中,然后转换为excel。我需要执行此操作而不创建任何功能。

import pandas as pd
import requests
import json
from pandas.io.json import json_normalize

df = pd.read_excel('C:/Users/one.xlsx',converters={'name':str}) 
df = df['name']
df.head()

dd=df.values
user=b"user"
passwd=b'pass'
auth_values = (user, passwd)
for i in dd:
    ur='http://xyz.co&name='
    url =ur + str(dd)
    response = (requests.get(url, auth=auth_values).text)
    response_json = json.loads(response)
    response_json=json.dumps(response,ensure_ascii=True)
    writeFile =open('C:/Users/file1.json', 'w')
writeFile.write(response_json)
writeFile.close()
print(url)
塞尔坎·阿尔斯兰

你可以试试看

import pandas as pd
import requests
import json
from pandas.io.json import json_normalize

df = pd.read_excel('C:/Users/one.xlsx',converters={'name':str}) 
df = df['name']
df.head()

dd=df.values
user=b"user"
passwd=b'pass'
auth_values = (user, passwd)

with open('C:/Users/file1.json', 'w') as writeFile: 
    for i in dd:
       ur='http://xyz.co&name='
       url =ur + str(i)
       response = requests.get(url, auth=auth_values).text
       response_json = json.loads(response)
       response_json=json.dumps(response,ensure_ascii=True)
       writeFile.write(response_json)
       print(url)

出口到Excel:

df = pd.read_excel('C:/Users/one.xlsx',converters={'name':str}) 
df = df['name']
dd=df.values

user=b"user"
passwd=b'pass'
auth_values = (user, passwd)

df_base = None
for i in dd:
    ur='http://xyz.co&name='
    url =ur + str(i)
    response = requests.get(url, auth=auth_values).text
    df = pd.read_json(response)

    if df_base is None:
        df_base = df
    else:
        df_base.append(df)

    print(url)

df_base.to_excel("C:/Users/output.xlsx")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章