Python爬蟲個人總結 持續更新
阿新 • • 發佈:2019-01-10
爬蟲中經常遇到這樣的程式碼:
ids=['id1','id2','id3']#爬蟲快取id資料
contents=['content1','content2','content3']#爬蟲快取內容資料
for id,conten in zip(ids,contents):
info={ #將id,content資訊存入info字典中
'content':content,
'id':id
}
info_lists.append(info)
等效於:
for id,conten in [('id1','content1'),('id2' ,'content2'),('id3','content3')]:
info={
'content':content,
'id':id
}
info_lists.append(info)
>>> a = [1,2,3] >>> b = [4,5,6] >>> c = [4,5,6,7,8] >>> zipped = zip(a,b) # 返回一個物件 >>> zipped <zip object at 0x103abc288> >>> list(zipped) # list() 轉換為列表 [(1, 4), (2, 5), (3, 6)] >>> list(zip(a,c)) # 元素個數與最短的列表一致 [(1, 4), (2, 5), (3, 6)]