我帶回了十多種Scrapy項目類型,將它們全部清理並存儲在SQL中的各個表中。我可以爲每個項目寫出說明,但看起來更加整潔,以編程方式管理各種列表/數據框/表格創建。使用存儲在字典中的字符串調用類
- 不幸的是,當我嘗試通過引用字典中的條目來調用scrapy項目時,Python將其讀取爲字符串而不是類型或類。
- 同樣,當我嘗試引用一個列表名 - Python仍然看到一個字符串不會讓我使用.append()。
任何獲得Python讀取字符串作爲類引用或列表引用的幫助將不勝感激。
這裏是我的代碼版本:
from scrapy import signals
from dealinfo.items import List, Details, Rd, Status, CompletedDetails, Syndicate
from dealinfo.items import CompanyDetails, CompanyContactInfo, CompanyTeam, Compadvisors, CompanyPInvestors
from dealinfo.items import CompanyExecSum, CurrentRd, PastRd, AnnFin#, CompanyDocs
import pandas as pd
from sqlalchemy import create_engine
class SQLPipeline(object):
engine=create_engine('mssql+pyodbc://username:[email protected]')
#### matrix of table names by type ######
prep = {'item_names': ['List', 'Details', 'Rd', 'Fin', 'Status', 'CompletedDetails', 'Syndicate', 'CompanyDetails', 'CompanyContactInfo', 'CompanyTeam', 'Compadvisors', 'CompanyPInvestors', 'CompanyExecSum', 'CompanyCurrentRd', 'CompanyPastRd', 'CompannFin', 'CompanyDocs'],
'temp_table': ['items_dl', 'items_dd', 'items_dr', 'items_df', 'items_nds', 'items_ncd', 'items_ns', 'items_cd', 'items_cci', 'items_ct', 'items_ca', 'items_cpi', 'items_es', 'items_cr', 'items_pr', 'items_af', 'items_cdoc'],
'data_frame': ['dl', 'dd', 'dr', 'df', 'nds', 'ncd', 'ns', 'cd', 'cci', 'ct', 'ca', 'cpi', 'es', 'cr', 'pr', 'af', 'cdoc'],
'sql_table': ['list', 'details', 'rd', 'fin', 'status', 'completed_details', 'syndicate', 'company_details', 'company_contact_info', 'company_team', 'company_advisors', 'company_pinvestors', 'company_execsum', 'company_current_rd', 'company_past_rd', 'company_ann_fin', 'company_docs']
}
#### assigning temporary lists for capturing parsed items ######
for x in prep['temp_table']:
globals()[x] = []
#### create sql schema to receive final output ######
def __init__(self):
try: ## Check schema exists, create if not
SQLPipeline.engine.execute("create schema dealinfo")
except:
pass
#### clean each scrapy item and add contents to temporary list (ahead of conversion to dataframe) ######
def process_item(self, item, spider):
for i in range(len(SQLPipeline.prep['item_names'])):
if isinstance(item, SQLPipeline.prep['item_names'][i]):####<<---error - not able to call item using string
for key,value in item.items():
if isinstance(item[key], list):
item[key] = [x.strip() for x in item[key] if x]
item[key] = [x for x in item[key] if x]
item[key] = ', '.join(item[key])
SQLPipeline.prep['temp_table'][i].append(item.copy())####<<---error - not able to call item using string
#### convert parsed items to pandas dataframe before sending to sql as tables ######
def close_spider(self, spider):
for i in SQLPipeline.prep['item_names']:
try:
SQLPipeline.prep['data_frame'][i] = pd.DataFrame(SQLPipeline.prep['temp_table'][i])
print(SQLPipeline.prep['data_frame'][i])
SQLPipeline.prep['data_frame'][i].to_sql(SQLPipeline.prep['sql_table'][i], SQLPipeline.engine, schema='dealinfo', if_exists='replace', index=False)
except Exception as ex:
print(ex)
pass
是的,*因爲您正在使用字符串*,您爲什麼期望有什麼不同?請提供一個[mcve] –
嗨 - 感謝您辦理登機手續 - 這是身份驗證牆背後的事情,所以我無法分享可用的代碼?無論如何,也許有通過字符串調用列表名稱和類的一般方法?這主要是我正在尋找的。 Thx再次。 –
編號請閱讀鏈接。你不提供你的實際代碼 - 證明你正在嘗試完成的[mcve] *,它證明了它的失敗。 –