【发布时间】:2022-01-05 13:14:05
【问题描述】:
我对编码很陌生,我需要解决一件实际的事情,只需从网站获取信息并将其写入 excel(我希望我可以通过指南进行管理),但主要问题是我无法进入网站(该网站是免费的) 你能看看我的代码吗?当我运行它时,我得到了
[] 进程以退出代码 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()
【问题讨论】:
-
Process finished with exit code 0- 这意味着您的代码没有错误。你已经完成了它,它没有任何问题。所以请关注并改进您的问题。谢谢 -
但是如果没问题,我应该在这一步的终端中获取一些数据 def get_content(html): soup = BeautifulSoup(html, 'html.parser') items = soup.find_all('a ', class_='抗肿瘤') print(items)
标签: python selenium selenium-webdriver python-class