2018-07-11晚上改的

# coding=utf-8

import requests

from bs4 import? BeautifulSoup

import re

import pymongo

import json

import pandas as pd

import numpy as np

import xlrd

import datetime

def onedayPriceSpider(spiderDay):


? ? url = "http://www.hfzgncp.com.cn/index.php?m=content&c=index&a=lists&catid=59&sendtime="+str(spiderDay)+"&page=1"

? ? headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0)' +\

? ? ? ? ? ? ? 'Gecko/20100101 Firefox/61.0'}

? ? r = requests.get(url,headers=headers)

? ? html = r.text.encode(encoding='utf_8').decode()

? ? soup = BeautifulSoup(html,"lxml")

? ? table = soup.find('table', attrs={'class' :{'h_list_table r_list_table'}})

? ? #print(table)

? ? #查看表格數(shù)據(jù)行數(shù)

? ? height = len(table.findAll(lambda tag:tag.name=='tr' and

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? len(tag.findAll('td'))>=1))

? ? print('height:'+str(height))

? ? #收集表頭

? ? columns = [x.text for x in table.tr.findAll('th')]

? ? columns = [x.replace('\xa0','') for x in columns]

? ? #print(columns)

? ? #構(gòu)造dataframe 準(zhǔn)備存儲表格

? ? width = len(columns)? #表頭列數(shù)

? ? #print('width:'+str(width))

? ? df = pd.DataFrame(data = np.full((height,width),'',dtype = 'U'),columns = columns)

? ? rows = [row for row in table.findAll('tr') if row.find('td')!=None]

? ? #逐行分析表格

? ? for i in range(len(rows)):

? ? ? ? cells = rows[i].findAll('td')

? ? ? ? if len(cells) == width:

? ? ? ? ? ? df.iloc[i] = [cell.text.replace(' ','').replace('\n','') for cell in cells]? #去點空格和換行

? ? ? ? else:

? ? ? ? ? ? w=len(cells)

? ? ? ? ? ? df.iloc[i,width-w:] = [cell.text.replace(' ','').replace('\n','') for cell in cells]

? ? #print(df)

? ? sendtime = soup.find('input', attrs={'id' :{'sendtime'}})['value'].rstrip('/-') #獲取數(shù)據(jù)時間

? ? sendtimeStr=re.sub("\-","",sendtime)

? ? #print(sendtimeStr)

? ? outputfilePath="D:/"+sendtimeStr+".xlsx"

? ? df.to_excel(outputfilePath)

? ? xlsx_data=xlrd.open_workbook(outputfilePath)

? ? xlsx_table=xlsx_data.sheet_by_name(u'Sheet1')

? ? columnIndex = None

? ? rowIndex = None?


? ? for j in range(xlsx_table.ncols):? ? ? ?

? ? ? ? for i in range(height):

? ? ? ? ? ? if(xlsx_table.cell_value(i, j) == '平均價'):

? ? ? ? ? ? ? ? columnIndex = j

? ? ? ? ? ? ? ? break? ? ? ?

? ? ? ? ? ? if(xlsx_table.cell_value(i, j) == '鯽魚'):

? ? ? ? ? ? ? ? rowIndex = i

? ? ? ? ? ? ? ? break

? ? print(xlsx_table.cell_value(rowIndex, columnIndex))


? ? #price_data = []

? ? #data.append(sheet.row_values(i))

? ? #client= pymongo.MongoClient()

? ? #獲取一個數(shù)據(jù)庫

? ? #db=client.priceSpider

? ? #創(chuàng)建 或獲取一個集合,并在collection下新建books

? ? #account=db.prcie

? ? #data=xlrd.open_workbook("D:/"+sendtimeStr+".xlsx")

? ? #table=data.sheets()[0]

? ? #讀取excel第一行數(shù)據(jù)作為存入mongodb的字段名

? ? #rowstag=table.row_values(0)

? ? #nrows=table.nrows

? ? #print('-------------nrows----------------'+str(nrows))

? ? #ncols=table.ncols #print rows

? ? #returnData={}

? ? #for i in range(1,nrows):

? ? ? ? #將字段名和excel數(shù)據(jù)存儲為字典形式,并轉(zhuǎn)換為json格式

? ? ? ? #returnData[i]=json.dumps(dict(zip(rowstag,table.row_values(i))))

? ? ? ? #通過編解碼還原數(shù)據(jù)

? ? ? ? #returnData[i]=json.loads(returnData[i])

? ? ? ? #print returnData[i]

? ? ? ? #account.insert(returnData[i])

? ? ? ? #return daySpider

def getColumnIndex(table, columnName):

? ? columnIndex = None? ?

? ? for i in range(table.ncols):? ? ? ?

? ? ? ? if(table.cell_value(0, i) == columnName):

? ? ? ? ? ? columnIndex = i

? ? ? ? break

? ? return columnIndex


spiderDaynumber =1

for i in range(spiderDaynumber):

? ? spiderDay = datetime.date.today()- datetime.timedelta(days=i+1)? ?

? ? print(spiderDay)

? ? onedayPriceSpider(spiderDay)

#xlsfile= 'F:/test_AutoTesting/TestCase/RunList.xlsx'

#? table = readExcelDataByName(xlsfile, 'Sheet1')[0]

? ? #獲取第一行的值? ?

? #? testcase_id = table.cell_value(1, getColumnIndex(table,'平均價'))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容