資源簡(jiǎn)介
適合爬蟲(chóng)初學(xué)者的必備入門demo,效果如下:
要求環(huán)境python3.7,安裝庫(kù)
requests,xlwt,re,os
采用新手友好的原始正則對(duì)文本進(jìn)行篩選,爬取豆瓣top250電影信息,生成excel表格,可在pycharm中直接運(yùn)行,
默認(rèn)保存目錄D:/test
代碼片段和文件信息
import?requests
import?xlwt
import?re
import?os
#保存數(shù)據(jù)生成excel表格
def?save_date(datalistsavepath):
?????if?not?os.path.exists(savepath):
?????????print(‘未存在文件夾‘savepath‘創(chuàng)建中‘)
?????????os.makedirs(savepath)
?????else:
?????????print(‘已存在文件夾,保存中‘)
?????savepath=savepath+‘/豆瓣top250電影.xls‘
?????book=xlwt.Workbook(encoding=‘utf-8‘style_compression=0)
?????sheet=book.add_sheet(“豆瓣電影Top248“cell_overwrite_ok=True)
?????col=(‘排名‘‘名稱‘‘詳細(xì)信息‘‘評(píng)分‘‘介紹‘)
?????for?i?in?range(05):
?????????sheet.write(0icol[i])
?????for?i?in?range(0len(datalist)):
??????????sheet.write(i+10datalist[i].get(‘index‘))
??????????sheet.write(i+1?1?datalist[i].get(‘title‘))
??????????sheet.write(i+1?2?datalist[i].get(‘info‘))
??????????sheet.write(i+1?3?datalist[i].get(‘score‘))
?????????
評(píng)論
共有 條評(píng)論