僅個人目前遇見的內存問題, 可能不適用所有問題
一下只是簡單的實例代碼, 可能跑不起來, 只是看看
可變變量參數
小例子:
def foo(a, b=[]): b.append(a) print b # input: foo(1) output: [1] # input: foo(2) output: [1,2]
解釋說明:
參考: http://tianshu.xyz/blog/82/
官方文檔中的一句話:
Default values are computed once, then re-used.
默認值是被重復使用的
Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call.
所以當默認參數值是可變對象的時候,那么每次使用該默認參數的時候,其實更改的是同一個變量
當python執行def語句時,它會根據編譯好的函數體字節碼和命名空間等信息新建一個函數對象,並且會計算默認參數的值。函數的所有構成要素均可通過它的屬性來訪問,比如可以用funcname屬性來查看函數的名稱。所有默認參數值則存儲在函數對象的defaults_屬性中,它的值為一個列表,列表中每一個元素均為一個默認參數的值
其中默認參數相當於函數的一個屬性
Functions in Python are first-class objects, and not only a piece of code.
我們可以這樣解讀:函數也是對象,因此定義的時候就被執行,默認參數是函數的屬性,它的值可能會隨着函數被調用而改變。其他對象不都是如此嗎?
其實使用 id(b) 的內存地址就很清楚了
解決:
def foo(a, b=None): b = b if b is not None else [] b.append(a) print b # 或者 def foo(b=None): b = b or []
使用pyquery
小例子:
import requests from pyquery import PyQuery def demo(url): html = requests.get(url).text print(html) pq_html = PyQuery(html) item_list = pq_html("#cm_cr-review_list>div[data-hook='review']") for item in item_list: iid = PyQuery(item)("div[data-hook='review']").attr("id") print(iid) if __name__ == "__main__": url = "評論頁面鏈接" demo(url)
項目說明
這個是項目中的一部分, 抓取評論的頁面:
在將整個頁面轉為PyQuery的對象后, 提取出評論列表, 然后遍歷評論列表, 再將每個評論的html轉為PyQuery對象, 也個頁面還好, 但是現在目前好像沒有單線程爬蟲了吧! 結果可想而知
解決方法
在看了PyQuery官方文檔后, 發現可以這樣:
item_list = pq_html("#cm_cr-review_list>div[data-hook='review']") for item in item_list.items(): iid = item("div[data-hook='review']").attr("id") print(iid)
也可以使用xpath
item_list = etree.HTML("#cm_cr-review_list>div[data-hook='review']") for item in item_list: iid = item.xpath("//div[@data-hook='review']/@id") print(iid)
# from lxml import etree
這樣直接使用對象來進行提取元素, 會節省很大一部分內存
使用requests session
小例子
import requests class Download(object): def __init__(self): self.session = requests.session() def down(self, url, try_time=10): while try_time: try: response = self.session.get(url) except Exception as e: print(e) finally: try_time -= 1
項目說明
在使用session的時候, 內存會直線上升, 並且沒有下降的趨勢, 直到內存崩潰, 特別是重試的時候, 目前沒有搞懂為什么session會這樣? 希望懂的大神解釋一下
解決
目前是不使用session, 直接使用requests.get()
求大神賜教
四. 持續更新......
以后遇見的bug, 或者內存泄漏問題都會記錄下來, 避免更多人踩坑 :)
也希望大家可以補充自己遇見的大坑
