简体   繁体   English

Pycharm上的PyQt5,未使用的模块

[英]PyQt5 on Pycharm, Modules not used

working on writing a Python script to scrape a webpage after it has run its JavaScript. 编写Python脚本来运行网页后抓取网页。 I realized I needed the JS to run because using the Requests wasn't returning any data. 我意识到我需要运行JS,因为使用Requests不会返回任何数据。 I found what seemed to be my solution here but I am having some problems still. 我发现这里似乎是我的解决方案但仍然存在一些问题。

First of all that tutorial uses PyQt4, I have installed and tried multiple versions of PyQt 4 and 5 from the project interpreter and still can't find a solution. 首先,该教程使用PyQt4,我已经从项目解释器安装并尝试了多个版本的PyQt 4和5,但仍然找不到解决方案。 Here is the relevant code: 以下是相关代码:

import PyQt5.QtWebEngineWidgets
import PyQt5.QtCore
import PyQt5.QtWebEngine
import PyQt5.QtWebEngineCore


class Render(QWebpage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QUrl(url))
        self.app.exec_()

    def _load_finished(self, result):
        self.frame = self.mainFrame()
        self.app.quit()

The QWebpage, QApplication, and QUrl calls all have 'Unresolved Reference' errors, the four PyQt5 import statements also all have 'Unused Import Statement' indications. QWebpage,QApplication和QUrl调用均具有“ Unresolved Reference”错误,四个PyQt5导入语句也均具有“ Unused Import Statement”指示。 I have tried for several hours to resolve these issues, uninstalling and reinstalling PyQt several times and searching the internet 我已经尝试了几个小时来解决这些问题,多次卸载并重新安装PyQt并搜索了互联网

Any advice would be awesome, Thanks! 任何建议都会很棒,谢谢!

Your imports are incorrect, in python there are many ways to do it: in your case you could be like this: 您的导入不正确,在python中有很多方法可以实现:在您的情况下,您可能会像这样:

1. from package import class 1. from package import class


import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWebKitWidgets import QWebPage
from PyQt5.QtWidgets import QApplication


# Take this class for granted.Just use result of rendering.
class Render(QWebPage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebPage.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QUrl(url))
        self.app.exec_()

    def _loadFinished(self, result):
        self.frame = self.mainFrame()
        self.app.quit()


url = 'http://pycoders.com/archive/'
r = Render(url)
result = r.frame.toHtml()
print(result)
  1. import package , then you should use each element as package.class : import package ,然后应将每个元素用作package.class

import sys
from PyQt5 import QtWebKitWidgets, QtCore, QtWidgets


class Render(QtWebKitWidgets.QWebPage):
    def __init__(self, url):
        self.app = QtWidgets.QApplication(sys.argv)
        QtWebKitWidgets.QWebPage.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.mainFrame().load(QtCore.QUrl(url))
        self.app.exec_()

    def _loadFinished(self, result):
        self.frame = self.mainFrame()
        self.app.quit()


url = 'http://pycoders.com/archive/'
r = Render(url)
result = r.frame.toHtml()
print(result)

If you are using pycharm there is a very simple way that pycharm imports the packages correctly for you, for this you must place the dot above the word that generates the error and execute Ctrl+M 如果您使用的是pycharm,有一种非常简单的方法可以让pycharm正确地为您导入软件包,为此,您必须将点放在产生错误的单词上方并执行Ctrl+M

Note:If you are using windows you will not be able to use these modules since Qt and therefore PyQt, use chromium, and it seems that they have a problem with Windows. 注意:如果您使用的是Windows,则由于Qt和PyQt,因此将无法使用这些模块,请使用铬,这似乎是Windows的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM