我已经写了一些代码在python与硒的帮助下,这是做网络爬行,它去一个网站输入登录id和密码后下载的文件。我在jupyter notebook中编写了文件名为GSPL_Code.ipynb
的代码,并使用!jupyter nbconvert --to script GSPL_Code.ipynb
和!pyinstaller GSPL_Code.py
,我首先将.ipynb
文件转换为.py
,然后从.py
文件生成.exe
文件。
如果我运行命名为GSPL_Code
的。exe文件,它存储在dist/GSPL_Code
中,然后在dist/GSPL_Code
文件夹中下载文件crv_report.xlsx
,其中可执行文件也在那里,但问题是代码在下载该位置的文件后不执行。它卡住了。现在什么也没发生。当我在jupyter notebook中运行时,我没有遇到任何问题,但在将其转换为.exe
后,我面临问题。
请找到以下python代码,这是整个脚本的一部分:
def handler(driver):
curr=driver.current_window_handle
for handle in driver.window_handles:
driver.switch_to.window(handle)
if handle != curr:
driver.close()
for handle in driver.window_handles:
driver.switch_to.window(handle)
print(driver.current_url)
click_event('//img[@id="IconImg_crv_report_toptoolbar_export"]')
click_event('//div[@style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px"]')
click_event('//span[@title="Microsoft Excel Workbook Data-only"]')
sleep(5)
click_event('//td[@class="wizbutton"]')
sleep(15)
print("Report is downloaded")
PATH1="C:\Users\ankit19.gupta\Desktop\Test_GSPL\dist\GSPL_Code"
crv_report= "/crv_report.xlsx"
print("Reading File")
dataframe1 = pd.read_excel(PATH1+crv_report)
print("Data is stored in the dataframe")
return dataframe1
我已经写了一些打印语句,看看错误是在哪里来的。因此,在这里我无法看到打印语句"Report is downloaded"
。在此之前,click_event('//td[@class="wizbutton"]')
是用来点击在chrome浏览器下载文件的按钮,我可以看到这个下载的文件在路径dist/GSPL_Code
目录下,它被成功执行,但之后它卡住了,什么也没发生。
我不知道这里发生了什么。有人能帮帮我吗?如有任何帮助,不胜感激。
编辑:由于打印报表"报告未下载",有一些延迟。and "读取文件"控制台没有显示,但现在我可以看到它,但它无法读取crv_report
文件,该文件位于路径PATH1
上,它被卡在这里,因此我无法看到打印语句Data is stored in the dataframe
我认为代码无法访问文件,因为当我试图删除crv_report
文件时,os.remove
也不起作用。有谁能帮帮我吗?
OS Directory Change:
当重构python文件时,在您将.py更改为.exe的情况下,重要的是要确保所有内容都是当前工作目录的一部分。。简单的操作系统检查器命令:
cwd = os.getcwd()
如果目录没有对齐,在与任何文件交互之前,您需要运行操作系统命令更改目录。:
os.chdir('/your/desired/directory')