我有一个下载网页的python脚本,解析它并从页面返回一些值.我需要抓一些这样的页面来获得最终结果.每个页面检索需要很长时间(5-10s),我宁愿并行提出请求以减少等待时间.
问题是 - 哪种机制可以快速,正确地执行,并且CPU /内存浪费最少?扭曲,异步,线程,其他什么?你能提供一些例子的链接吗?
谢谢
UPD:这个问题有一些解决方案,我正在寻找速度和资源之间的妥协.如果你能告诉一些经验细节 - 从你的观点来看它是如何快速负载 - 这将是非常有帮助的.
在Twisted中有1天的经验我尝试安排消息发送以回复tcp客户端:
import os, sys, time
from twisted.internet import protocol, reactor
self.scenario = [(1, "Message after 1 sec!"), (4, "This after 4 secs"), (2, "End final after 2 secs")]
for timeout, data in self.scenario:
reactor.callLater(timeout, self.sendata, data)
print "waited %d time, sent %s\n"%(timeout, data)
Run Code Online (Sandbox Code Playgroud)
现在它发送消息,但我有2个问题:
1)"超时"从"现在"开始,我想在每个上一个任务完成后计算它(之前的消息被发送)
2)我不知道如何发送所有消息后关闭连接.如果我放在s self.transport.loseConnection()之后callLater立即关闭连接.
在以前的尝试我没有用reactor.callLater,但只self.transport.write()和time.sleep(n)在for循环.在这种情况下,所有消息都在所有超时后一起发送...不是我想要的东西.
目的是等待客户端连接,等待timeout1并发送message1,等待timeout2并发送message2,...等.最后消息后 - 关闭连接.
我尝试使用 Linux 机器中的 python 连接到 MSSQL DB (Python 2.7、Ubuntu 11.04)。我收到的输出被截断为 500 个字符。请参阅下面的脚本和配置。怎么解决呢?我认为问题出在 ODBC 驱动程序中或附近。
代码(pyodbc、pymssql):
conn = pymssql.connect(host='my_remote_host', user='ro_user',
password='password', database='current', as_dict=True)
cur = conn.cursor()
cur.execute(sql)
for i in cur:
print i
conn.close()
cnxn = pyodbc.connect(driver='FreeTDS', server='my_remote_host', database='current', uid='ro_user', pwd='password')
cursor = cnxn.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
...
cnxn.close()
Run Code Online (Sandbox Code Playgroud)
我没有对 MS SQL DB 的写访问权限,它实际上是不属于我们系统的远程服务器。
sql = '''
SELECT Req.ID,
ShReq.Summary AS [Short Name],
ShReq.ALM_SharedText AS [Text],
Req.ContainedBy,
Req.DocumentID
FROM CurMKS..ALM_Requirement Req
JOIN CurMKS..ALM_SharedRequirement ShReq …Run Code Online (Sandbox Code Playgroud) 我尝试组织最多10个并发下载的池。该功能应下载基本URL,然后解析该页面上的所有URL并下载每个URL,但是并发下载的总数量不应超过10。
from lxml import etree
import gevent
from gevent import monkey, pool
import requests
monkey.patch_all()
urls = [
'http://www.google.com',
'http://www.yandex.ru',
'http://www.python.org',
'http://stackoverflow.com',
# ... another 100 urls
]
LINKS_ON_PAGE=[]
POOL = pool.Pool(10)
def parse_urls(page):
html = etree.HTML(page)
if html:
links = [link for link in html.xpath("//a/@href") if 'http' in link]
# Download each url that appears in the main URL
for link in links:
data = requests.get(link)
LINKS_ON_PAGE.append('%s: %s bytes: %r' % (link, len(data.content), data.status_code))
def get_base_urls(url):
# Download the …Run Code Online (Sandbox Code Playgroud) 嗨,
我需要过滤掉所有不包含来自巨大"必要"列表的符号的行,示例代码:
def any_it(iterable):
for element in iterable:
if element: return True
return False
regexp = re.compile(r'fruit=([A-Z]+)')
necessary = ['YELLOW', 'GREEN', 'RED', ...] # huge list of 10 000 members
f = open("huge_file", "r") ## file with > 100 000 lines
lines = f.readlines()
f.close()
## File rows like, let's say:
# 1 djhds fruit=REDSOMETHING sdkjld
# 2 sdhfkjk fruit=GREENORANGE lkjfldk
# 3 dskjldsj fruit=YELLOWDOG sldkfjsdl
# 4 gfhfg fruit=REDSOMETHINGELSE fgdgdfg
filtered = (line for line in lines if any_it(regexp.findall(line)[0].startswith(x) …Run Code Online (Sandbox Code Playgroud) 我有这样的文件用Python解析(从报废):
some HTML and JS here...
SomeValue =
{
'calendar': [
{ 's0Date': new Date(2010, 9, 12),
'values': [
{ 's1Date': new Date(2010, 9, 17), 'price': 9900 },
{ 's1Date': new Date(2010, 9, 18), 'price': 9900 },
{ 's1Date': new Date(2010, 9, 19), 'price': 9900 },
{ 's1Date': new Date(2010, 9, 20), 'price': 9900 },
{ 's1Date': new Date(2010, 9, 21), 'price': 9900 },
{ 's1Date': new Date(2010, 9, 22), 'price': 9900 },
{ 's1Date': new Date(2010, 9, 23), 'price': …Run Code Online (Sandbox Code Playgroud)
我有两个数据集作为列表,例如:
xa = [1, 2, 3, 10, 1383, 0, 12, 9229, 2, 494, 10, 49]
xb = [1, 1, 4, 12, 1100, 43, 9, 4848, 2, 454, 6, 9]
Run Code Online (Sandbox Code Playgroud)
系列是可能包含数万个数字的市场数据,其长度相同.
我需要找到百分比中的"差异",它表示"百分比系列之间有多少相似性/相异性".
目前我有想法为每个列表构建图表(xa,xb为Y ax,范围(1,len(xa))为X ax).插入xa,xb的函数,然后计算xa,xb(带积分)的面积和xa和xb之间的差异面积.此后,不相似度为(差异区域)*100%/(xa面积+ xb面积).
我想知道这个问题是否有更简单的解决方案.如果不是 - 我怎样才能计算出xa,xb的差异面积?图表是用scipy,numpy,matplotlib构建的.
更新:我正在寻找代表集合之间差异的一个数字.百分之是优选的.
python ×7
dataset ×1
filter ×1
gevent ×1
html-parsing ×1
json ×1
math ×1
networking ×1
numpy ×1
odbc ×1
optimization ×1
pool ×1
pymssql ×1
pyodbc ×1
python-2.4 ×1
scipy ×1
sql-server ×1
text ×1
twisted ×1
web ×1
web-scraping ×1