我正在尝试编写我的第一个 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 保存在管理器中,并将它们传递给工作人员。
#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(我认为)想要将二进制文件移回管理器进行处理,并且我收到一条“无法腌制对象”错误消息,指的是它试图读取文件的网页。
显而易见的解决方案是从 cookie jar 中读取 cookie,并在发出 urlretrieve 请求时手动将它们添加到标头中,但我试图避免这种情况,这就是我寻求建议的原因。