繁体   English   中英

Unable to scrape WebPage using class 实现 selenium

[英]Unable to scrape WebPage using class Implementation of selenium

我正在使用selenium来抓取 web 页面,该页面由 javascript 动态生成。当我直接从 cmd(python) 终端调用时,它工作正常。 但是当我在 class 中实现此功能时,效果不佳。

我的 class 实现是:

    class web_scraper():
        def __init__(self):
            # start chrome driver 
            self.driver = webdriver.Chrome(executable_path="./config/chromedriver.exe")
        
       # scrape web page from specified url
        def scrape_page(self, url):
            html = None
            try:
                # scrape page
                self.driver.get(url)
                
                # read html 
                html = self.driver.execute_script("return document.documentElement.innerHTML;")
            except Exception as e:
                print('[Error:] Scrapping failed.')
                print(f'[Exception:] {e}')
    
            return html
     if __name__ == '__main__':
         url = "https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxPage9"
         scraper = web_scraper()
         content = scraper.scrape_page(url)

我在终端使用的代码是:

driver = webdriver.Chrome(executable_path='E:/Projects/Python_Projects/WebScraping/config/chromedriver.exe')
driver.get("https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxPage30")
content = driver.execute_script("return document.documentElement.innerHTML;")

class 实现的 Output 是:

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link type="text/css" rel="stylesheet" href="Wipp.css">
    <title>WIPP</title>
  <link rel="stylesheet" href="https://wipp.edmundsassoc.com/Wipp/wipp/gwt/standard/standard.css"><script src="https://wipp.edmundsassoc.com/Wipp/wipp/0D3421F8F9508D2F958C63CE2A48BAD8.cache.js"></script></head>

  <body>
    <script type="text/javascript" language="javascript" src="wipp/wipp.nocache.js"></script>
    <iframe src="javascript:''" id="__gwt_historyFrame" tabindex="-1" style="position:absolute;width:0;height:0;border:0"></iframe>


</body>

而对于 python 终端上的命令,output 就可以了。

对此的任何帮助将不胜感激。 谢谢!

我正在使用 Windows 操作系统和 Python 版本是 3.6。

得到url后添加time.sleep()

self.driver.get(url)
time.sleep(10)

暂无
暂无

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

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