Python:替换excel中for循环中的名称



我有一个电子表格,里面有一列CIK数字和一列url。我想为url列创建一个for循环,并为每个url获得一个excel输出,该输出被命名为相应的CIK编号。我在这个问题上纠结了很长时间。谢谢你的帮助!

ExcelSpreadsheet

import urllib.request as urllib2
from bs4 import BeautifulSoup
import re
import requests
import os
import time
from pandas import DataFrame
import pandas as pd
from urllib.request import urlopen

#headers={"Content-Type":"text"}
headers = {'User-Agent': 'moxiao@msu.edu'}
# open the file
data=pd.read_excel('/content/experiment.xlsx')
# get the urls
urls=data.URL
CIK=data.CIK


for CIK, url in CIK, urls:
Cash=[]
response = requests.get(url, headers = headers)
response.raise_for_status()
time.sleep(0.1)
soup = BeautifulSoup(response.text,'lxml')
for table in soup.find_all('table'):
for tr in table.find_all('tr'):
for row_num in range(len(tr)): 
row = [] 
for td in tr.find_all('td'):
for row_item in td.get_text(strip=True):
try:
if row_item[0] == 'Cash and cash equivalent':
aa = re.sub("(xa0)|(n)|,","",row_item.text)
row.append(aa)
except IndexError:
continue
Cash.append(row)
df = pd.DataFrame(data=Cash)
df.to_excel(f'/content/{CIK}.xlsx')


for CIK, url in CIK, urls不是有效的Python构造。你是说吗

for single_CIK, url in zip(CIK, urls)

最新更新