【发布时间】:2013-11-01 10:44:44
【问题描述】:
我正在尝试编写我的第一个 Python 脚本,并且通过大量的谷歌搜索,我认为我即将完成。不过,我需要一些帮助才能越过终点线。
我需要编写一个脚本,登录到启用 cookie 的站点,抓取一堆链接,然后生成一些进程来下载文件。我的程序在单线程中运行,所以我知道代码可以工作。但是,当我尝试创建一个下载工作者池时,我碰上了一堵墙。
#manager.py
import Fetch # the module name where worker lives
from multiprocessing import pool
def FetchReports(links,Username,Password,VendorID):
pool = multiprocessing.Pool(processes=4, initializer=Fetch._ProcessStart, initargs=(SiteBase,DataPath,Username,Password,VendorID,))
pool.map(Fetch.DownloadJob,links)
pool.close()
pool.join()
#worker.py
import mechanize
import atexit
def _ProcessStart(_SiteBase,_DataPath,User,Password,VendorID):
Login(User,Password)
global SiteBase
SiteBase = _SiteBase
global DataPath
DataPath = _DataPath
atexit.register(Logout)
def DownloadJob(link):
mechanize.urlretrieve(mechanize.urljoin(SiteBase, link),filename=DataPath+'\\'+filename,data=data)
return True
在此修订版中,代码失败是因为 cookie 尚未传输给工作人员以供 urlretrieve 使用。没问题,我可以使用 mechanize 的 .cookiejar 类将 cookie 保存在 manager 中,并将它们传递给 worker。
#worker.py
import mechanize
import atexit
from multiprocessing import current_process
def _ProcessStart(_SiteBase,_DataPath,User,Password,VendorID):
global cookies
cookies = mechanize.LWPCookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
Login(User,Password,opener) # note I pass the opener to Login so it can catch the cookies.
global SiteBase
SiteBase = _SiteBase
global DataPath
DataPath = _DataPath
cookies.save(DataPath+'\\'+current_process().name+'cookies.txt',True,True)
atexit.register(Logout)
def DownloadJob(link):
cj = mechanize.LWPCookieJar()
cj.revert(filename=DataPath+'\\'+current_process().name+'cookies.txt', ignore_discard=True, ignore_expires=True)
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cj))
file = open(DataPath+'\\'+filename, "wb")
file.write(opener.open(mechanize.urljoin(SiteBase, link)).read())
file.close
但是,这失败了,因为 opener(我认为)想要将二进制文件移回管理器进行处理,并且我收到“无法腌制对象”错误消息,指的是它试图读取文件的网页.
显而易见的解决方案是在发出 urlretrieve 请求时从 cookie jar 中读取 cookie 并手动将它们添加到标头中,但我试图避免这种情况,这就是我寻求建议的原因。
【问题讨论】:
标签: python cookies urllib2 multiprocessing urllib