2010-12-19 60 views
1

所以我有這個python代碼,通過一個美味的頁面運行,並從中刪除了一些鏈接。提取方法包含一些提取所需內容的魔法。然而,運行頁面一個接一個地讀取相當慢 - 是否有一種方法可以在python中執行此異步操作,以便我可以同時啓動多個獲取請求和處理頁面?Python +機械化異步任務

url= "http://www.delicious.com/search?p=varun" 
page = br.open(url) 
html = page.read() 
soup = BeautifulSoup(html) 
extract(soup) 

count=1 
#Follows regexp match onto consecutive pages 
while soup.find ('a', attrs={'class': 'pn next'}): 
    print "yay" 
    print count 
    endOfPage = "false" 
    try : 
     page3 = br.follow_link(text_regex="Next") 
     html3 = page3.read() 
     soup3 = BeautifulSoup(html3) 
     extract(soup3) 
    except: 
     print "End of Pages" 
     endOfPage = "true" 
    if valval == "true": 
     break 
    count = count +1 
+0

看看「線程」和「多處理」。 – katrielalex 2010-12-19 00:56:41

+0

是否有任何特別的框架可以很好地與機械化和BeautifulSoup配合使用? – varunsrin 2010-12-19 02:15:34

回答

1

美麗的湯是相當緩慢的,如果你想更好的使用性能,而不是LXML或者如果你有很多CPU的或許你可以嘗試使用帶有隊列多處理。