pyinstaller ModuleNotFoundError:'fake_useragent.data'

问题描述 投票:0回答:1

最重要的是,感谢您的光临。

我正在做网页抓取并将其制作成.exe文件 代码运行良好,但不在 .exe 文件中

我想要的是:工作exe文件,避免机器人检测

我在做什么:

我正在进行网络抓取。 由于机器人检测,我使用 fake_useragent

from ipypb import ipb
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
# from user_agent import generate_user_agent

import requests
import urllib.parse
import time
import random




def download(dir_to_save, url_to_download) :


    # Instantiate the UserAgent class
    ua = UserAgent()
    
    # Get random user agents
    random_ua = ua.random
    
    
    headers = {
    
    'user-agent': random_ua
    # "User-Agent": generate_user_agent(os='win',device_type='desktop')
    # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"

    }

没有 fake_useragent .exe 文件也可以工作,但会进入机器人检测,猜测速度较慢

这是错误消息

Unable to find local data/json file or could not parse the contents using importlib-resources. Try pkg-resource next.
Traceback (most recent call last):
  File "fake_useragent\utils.py", line 21, in load
  File "importlib\resources\_common.py", line 22, in files
  File "importlib\resources\_common.py", line 53, in get_package
  File "importlib\resources\_common.py", line 44, in resolve
  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'fake_useragent.data'
Could not find local data/json file or could not parse the contents using pkg-resource.
Traceback (most recent call last):
  File "fake_useragent\utils.py", line 21, in load
  File "importlib\resources\_common.py", line 22, in files
  File "importlib\resources\_common.py", line 53, in get_package
  File "importlib\resources\_common.py", line 44, in resolve
  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'fake_useragent.data'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "fake_useragent\utils.py", line 36, in load
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\sa031\\AppData\\Local\\Temp\\_MEI180322\\fake_useragent\\data\\browsers.json'
Exception in Tkinter callback
Traceback (most recent call last):
  File "tkinter\__init__.py", line 1948, in __call__
  File "download.py", line 81, in <lambda>
  File "main_final.py", line 18, in download
  File "fake_useragent\fake.py", line 76, in __init__
  File "fake_useragent\utils.py", line 52, in load
fake_useragent.errors.FakeUserAgentError: ('Data list is empty', None)

我试过了

1.谷歌一下 2.尝试在stackoverflow中查找提示 3. 在命令中使用 pyinstaller 和 --onefile 选项 4.进入官方页面。

最接近的问题在这里

找不到pyinstaller模块fake_useragent

但是答案中的解决方案不再支持。

如果有人给我建议那就太好了。

我的第一个实际程序就快到了。

感谢您的阅读。

web-scraping web-crawler pyinstaller user-agent
1个回答
0
投票

实际上,您过去发布的链接在2024年8月收到了新答案。这个:https://stackoverflow.com/a/78876788/9151070

建议在pyinstaller中使用

--collect-datas=fake_useragent
。我遇到了和你一样的问题,这个建议解决了它。示例:

pyinstaller --collect-datas=fake_useragent --onefile -w my_project.py
© www.soinside.com 2019 - 2024. All rights reserved.