Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 面向对象的方法是否适合我的任务?如果是,如何实施的大致思路?_Python 3.x_Selenium - Fatal编程技术网

Python 3.x 面向对象的方法是否适合我的任务?如果是,如何实施的大致思路?

Python 3.x 面向对象的方法是否适合我的任务?如果是,如何实施的大致思路?,python-3.x,selenium,Python 3.x,Selenium,目前正在尝试为我的工作开发一些其他人可以使用的东西。我知道面向对象的方法被认为是很重要的,所以我试图设想如何将其用于我正在尝试做的事情,但没有看到如何使用 我正在使用Selenium用python编写一个web scraper。有一些数据表可以为不同的客户端访问,我想让我的程序的未来用户拉一个或多个表来查看数据,或者使用它来验证网站上的内容是否正确填充 代码仍在进行中,我正在尝试学习最佳实践和正确的方法。欢迎任何其他反馈,我想学习 from selenium import webdriver f

目前正在尝试为我的工作开发一些其他人可以使用的东西。我知道面向对象的方法被认为是很重要的,所以我试图设想如何将其用于我正在尝试做的事情,但没有看到如何使用

我正在使用Selenium用python编写一个web scraper。有一些数据表可以为不同的客户端访问,我想让我的程序的未来用户拉一个或多个表来查看数据,或者使用它来验证网站上的内容是否正确填充

代码仍在进行中,我正在尝试学习最佳实践和正确的方法。欢迎任何其他反馈,我想学习

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import getpass
import bs4
import time

###Open a headless Chrome, grab the URL page, enter into username and password, POST
chop = Options()
driver = input().strip()
if driver == "y":
     chop.add_argument("--headless")
driver = webdriver.Chrome(r"<Path to Chromedriver.exe placeholder>", options=chop)
driver.get("<URL placeholder>")
element = driver.find_element_by_name("email")
element.clear()
element.send_keys(input("Username: ").strip())
element = driver.find_element_by_name("password")
element.clear()
element.send_keys(getpass.getpass().strip())
element.send_keys(Keys.ENTER)

###Select a client, navigate to a table
select = Select(driver.find_element_by_id("<ID placeholder>"))
select.select_by_value(input("Client #:").strip())
element = driver.find_element_by_id("<ID placeholder>")
element.click()
element = driver.find_element_by_id("<ID placeholder>")
element.click()

###Attempt to grab the entire table, print it out in terminal
###1 second sleep semi-necessary to give page time to load table
time.sleep(1)
element = driver.find_elements(By.TAG_NAME, "tr")

###Print out grabbed rows
for L in element[2:-2]: 
    print(L.text)

driver.quit()
到目前为止,它只允许您导航到其中一个表并获取所有行。有些行不是真正有效的,所以这就是为什么我要切片。占位符只是为了隐藏我的实际工作内容,我想公司不会希望一群随机的人找到网站。

selenium已经使用面向对象的方法实现了。OOP的设计目的是帮助减少尝试扩展具有额外需求的项目的压力,或者在您的情况下,假设我想创建一个额外的网站,您必须为每个网站编写一个独特的脚本。我对selenium进行抽象的方法之一是创建一个管理selenium所有开销的解析器,然后使用负责抓取单个网站并返回其结果的解析器列表进行初始化。下面的代码可能更好地解释了这一点:

类别网页管理员: def parsebrowser:webdriver.Firefox: 处理此特定网页的唯一脚本 ... SeleniumController类: 定义初始化解析器:列表[WebPasser]: self.parsers=解析器 self.browser=webdriver.FireFox 在此处或在单独的函数中完成浏览器初始化 ... def触发器: 结果=[] 对于self.parsers中的p: result.appendp.parself.browser 处理你的结果 ... 然后,您可以为需要解析的每个不同网页创建WebPasser的子类


这类问题还可能得益于在运行时使用一个或某种类型的工具来帮助创建web解析器。是一个将面向对象的概念应用于各种问题的令人惊叹的网站

谢谢您提供的信息,稍后我将介绍重构浣熊。如果我要创建WebPasser的子类,那么SeleniumController初始化中使用的WebPasser列表是否会接受WebPasser的派生类?是的,因为基类仍然是WebPasser,所以方法parse仍然存在,您也可以将parsers:list[WebPasser]替换为parsers:list它不重要,我只是展示了guidanceI的类型,我投票将这个问题作为主题外的问题来结束,因为它要求进行代码审查,这更适合@GregBurghardt。我想问的是,如何将面向对象的方法应用于某种程度上使用web刮板,如果这被认为是代码审查,那么我将关闭它。