【发布时间】:2014-05-11 09:28:10
【问题描述】:
我正在研究大数据的网络抓取,因此我编写了以下代码以从我们校园的本地服务器获取一些信息。它工作正常,但我认为性能很慢;每条记录需要 0.91 秒才能存储到数据库中。代码所做的是打开一个网页,获取一些内容并将其存储在磁盘上。
我的目标是将抓取记录所用的时间减少到接近 0.4 秒(或更少,如果可能的话)。
#!/usr/bin/env python
import scraperwiki
import requests
import lxml.html
for i in range(1, 150):
try:
html = requests.get("http://testserver.dc/"+str(i)"/").content
dom = lxml.html.fromstring(html)
for entry in dom.cssselect('.rTopHeader'):
name = entry.cssselect('.bold')[0].text_content()
for entry in dom.cssselect('div#rProfile'):
city = entry.cssselect('li:nth-child(2) span')[0].text_content()
for entry in dom.cssselect('div#rProfile'):
profile_id = entry.cssselect('li:nth-child(3) strong a')[0].get('href')
profile = {
'name':name,
'city':city,
'profile_id':profile_id
}
unique_keys = [ 'profile_id' ]
scraperwiki.sql.save(unique_keys, profile)
print jeeran_id
except:
print 'Error: ' + str(i)
【问题讨论】:
-
您有 3 个地方消耗时间:http 获取、dom 操作和数据库保存。你应该做的是首先只使用 get 进行循环,然后使用 get 和 dom 操作但不保存数据库,最后进行完全处理。这将为您提供可以赢得时间的地方。
-
在某些时候,对于每条记录的抓取速度,您可能无能为力,因为这取决于您的网络延迟。您可以考虑通过多线程并行进行抓取。
标签: python web-scraping scraperwiki