如何将更新的页面内容传递给其他功能? - python

我有一些用于selenium-webdriver的代码,如下所示:

 base_url = 'http://wsprnet.org/drupal/wsprnet/spotquery'
 driver = webdriver.Chrome()
 driver.get(base_url)

在输入登录名和密码后,页面将根据以下代码进行更新:

btn_elem_upd = driver.find_element_by_id('edit-submit').click()

接下来需要将更新后的页面转移到另一个功能,如下所示:

url = "Here need transfer the new updated page"
res = requests.get(url)
doc = lxml.html.fromstring(res.text)

如何才能做到这一点?有什么选择?
更新,如果我这样写,代码可以工作:

new_source = 'http://wsprnet.org/drupal/wsprnet/spots'
res = requests.get(new_source)

doc = lxml.html.fromstring(res.text)

cols = []

cols.append(doc.xpath('//table/tr[1]/node()/text()')[0])
for item in doc.xpath('//table/tr/th'):
    typ = str(type(item.getnext()))
    if not 'NoneType' in typ:
        cols.append(item.getnext().text)

inf = []
for item in doc.xpath('//table//tr//td'):
    inf.append(item.text.replace('\\xa02', '').strip()) 

rows = [inf[x:x+len(cols)] for x in range(0, len(inf), len(cols))]

with open("output.csv", "w", newline='') as f:
    writer = csv.writer(f)
    writer.writerow(cols)
    for l in rows:
        writer.writerow(l)

如果我重写:

doc = lxml.html.fromstring(driver.page_source)
cols = []
cols.append(doc.xpath('//table/tr[1]/node()/text()')[0])

该代码无法正常工作:

cols.append(doc.xpath('//table/tr[1]/node()/text()')[0])
IndexError: list index out of range

参考方案

根据我们的讨论,您可以使用pandas库读取表信息并将其加载到csv文件中。

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
import pandas as pd

driver = webdriver.Chrome()
driver.get("http://wsprnet.org/drupal/wsprnet/spotquery")
login = driver.find_element_by_id('edit-name')
password = driver.find_element_by_id('edit-pass')
btn_elem_log = driver.find_element_by_id('edit-submit--2')
login.send_keys('username')
password.send_keys('password')
btn_elem_log.click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"edit-call"))).send_keys("searchval")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"edit-excludespecial"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"edit-submit"))).click()
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.TAG_NAME,"table")))
page=driver.page_source
df=pd.read_html(page)
df[0].to_csv("csvfile.csv",index=False)

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Selenium with Python:从具有只读功能的表单中收集电子邮件 - python

我正在尝试从内部具有只读内容的网站上的表单收集电子邮件地址。<input name="email" id="email" type="text" class="form-control" value="[email protected]" readonl…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…