简体   繁体   English

如何在 Python class 中调用方法

[英]How to call a method within a Python class using Selenium and Python

I am very new to coding, I need to solve one practical thing just get info from site and write it to excel(i hope i can manage with by guides), but the main issue i just cant get into the site(the website is free) Can you look over my code?我对编码很陌生,我需要解决一件实际的事情,只需从网站获取信息并将其写入 excel(我希望我可以通过指南进行管理),但主要问题是我无法进入网站(网站是免费)你能看看我的代码吗? When i run it, i get当我运行它时,我得到

[] Process finished with exit code 0 [] 进程以退出代码 0 结束

import requests
from bs4 import BeautifulSoup
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

URL ='http://way2drug.com/passonline/'
HEADERS= {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'}


class Test1():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_1(self):
    # Test name: 1
    # Step # | name | target | value | comment
    # 1 | open | /passonline/ |  | 
    self.driver.get("http://way2drug.com/passonline/")
    # 2 | setWindowSize | 1920x1030 |  | 
    self.driver.set_window_size(1920, 1030)
    # 3 | click | css=#registration img |  | 
    self.driver.find_element(By.CSS_SELECTOR, "#registration img").click()
    # 4 | click | name=user_login |  | 
    self.driver.find_element(By.NAME, "user_login").click()
    # 5 | type | name=user_login |  | 
    self.driver.find_element(By.NAME, "user_login").send_keys("MY USER")
    # 6 | click | id=page1 |  | 
    self.driver.find_element(By.ID, "page1").click()
    # 7 | type | name=user_password |  | 
    self.driver.find_element(By.NAME, "user_password").send_keys("MY PASS")
    # 8 | click | id=register |  | 
    self.driver.find_element(By.ID, "register").click()
    # 9 | click | id=myHeader1 |  | 
    self.driver.find_element(By.ID, "myHeader1").click()
    # 10 | click | id=smiles |  | 
    self.driver.find_element(By.ID, "smiles").click()
    self.driver.find_element(By.ID, "smi").click()
    self.driver.find_element(By.ID, "smi").send_keys("CC1(C)C(O)CC[C@@]2(C)C1CC[C@]3(C)C2CCC4[C@@]3(C)CC[C@]5(C(O)=O)C4[C@H](C)C(C)=CC5")
    self.driver.find_element(By.CSS_SELECTOR, "#myContent4 input:nth-child(4)").click()



def get_html(url, params=None):
  r = requests.get(url, headers=HEADERS, params=params)
  return r

def get_content(html):
  soup = BeautifulSoup(html, 'html.parser')
  items = soup.find_all('a', class_='Antineoplastic')

  print(items)

def parse():
  html = get_html(URL)
  if html.status_code == 200:
    get_content(html.text)
  else:
    print('ALL YOUR BASE ARE BELONG TO US')


parse()

Please note - Never ever provide credentials请注意-永远不要提供凭据

What happens?怎么了?

You mentioned you have to perform a login and selenium is a good choice, but what you are doing is to call parse() that only performce a call via requests .您提到您必须执行登录,并且 selenium 是一个不错的选择,但是您正在做的是调用parse()仅通过requests执行调用。 So if you take a look into your soup you wont find what you are looking for.所以如果你看一下你的soup ,你不会找到你要找的东西。

How to fix?怎么修?

Perform your selenium actions and walk to the website you want to scrape.执行您的 selenium 操作并步行到您要抓取的网站。 In next step push your driver.page_source into BeautifulSoup and find your elements:在下一步中,将您的driver.page_source推送到BeautifulSoup并找到您的元素:

soup = BeautifulSoup(driver.page_source,'html.parser')
items = soup.find_all('a', class_='Antineoplastic')

print(items)

If your selection is right, you will get your result.如果你的选择是正确的,你就会得到你的结果。

EDIT编辑

Concerning your comments a clue where you can end up, for debugging steps between, you should ask as new question with focused examples:关于您的评论,您可以在哪里结束,对于之间的调试步骤,您应该通过重点示例提出新问题:

import requests
from bs4 import BeautifulSoup
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Chrome()
 
driver.get("http://way2drug.com/passonline/")
driver.set_window_size(1920, 1030)
driver.find_element(By.CSS_SELECTOR, "#registration img").click()
driver.find_element(By.NAME, "user_login").click()
driver.find_element(By.NAME, "user_login").send_keys("MY USER")
driver.find_element(By.ID, "page1").click()
driver.find_element(By.NAME, "user_password").send_keys("MY PASS")
driver.find_element(By.ID, "register").click()
driver.find_element(By.ID, "myHeader1").click()
driver.find_element(By.ID, "smiles").click()
driver.find_element(By.ID, "smi").click()
driver.find_element(By.ID, "smi").send_keys("CC1(C)C(O)CC[C@@]2(C)C1CC[C@]3(C)C2CCC4[C@@]3(C)CC[C@]5(C(O)=O)C4[C@H](C)C(C)=CC5")
driver.find_element(By.CSS_SELECTOR, "#myContent4 input:nth-child(4)").click()

soup = BeautifulSoup(driver.page_source,'html.parser')
items = soup.find_all('a', class_='Antineoplastic')

print(items)

driver.quit()

When using you have to take care of a few things:使用时,您必须注意以下几点:

  • Once you define the class methods, you have to create an instance of the class within main to invoke the methods as follows:定义 class 方法后,必须在main中创建 class 的实例以调用方法,如下所示:

     test1 = Test1() test1.setup_method()
  • While defining the method, the arguments needs to match with the number/type of arguments you pass as parameters from the main.在定义方法时,arguments 需要与您作为参数从主传递的 arguments 的数量/类型匹配。 As an example:举个例子:

     def test_1(self):

    Should be called as:应称为:

     test1.test_1()

This usecase这个用例

From your entire code block I have taken a few blocks and modified a bit to demonstrate the implementation/usage of Classes :从您的整个代码块中,我采取了一些块并进行了一些修改以演示Classes的实现/使用:

Code Block:代码块:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

class Test1():
  def setup_method(self):
    print("Within setup_method")
    s = Service('C:\\BrowserDrivers\\chromedriver.exe')
    self.driver = webdriver.Chrome(service=s)


  def teardown_method(self):
    print("Within teardown_method")
    self.driver.quit()

  def test_1(self):
    print("Within test_1") 
    self.driver.get("http://way2drug.com/passonline/")


test1 = Test1()
test1.setup_method()
test1.test_1()
test1.teardown_method()

Console Output:控制台 Output:

Within setup_method
Within test_1
Within teardown_method

References参考

You can find a couple of relevant detailed discussion in:您可以在以下位置找到一些相关的详细讨论:

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

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