python - 机械汤python填充登录字段失败
问题描述
我正在尝试使用机械汤登录 Sherwin Williams 承包商网站。我让这个脚本工作了很长一段时间,然后发生了一些变化,导致我不再能够在登录页面上输入我的登录凭据。这是我的脚本:
import mechanicalsoup, requests, csv, os.path, pathlib
from datetime import datetime
today = datetime.today().strftime('%m-%d-%Y')
#create stateful browser
browser = mechanicalsoup.StatefulBrowser(
soup_config={'features': 'lxml'},
raise_on_404=True,
user_agent='MyBot/0.1: mysite.example.com/bot_info',
)
user = ###
pw = ###
# use browser to open link
browser.open("https://www.sherwin-williams.com/painting-contractors")
browser.follow_link("#Header_GlobalLogin_signInQuickLink")
form = browser.select_form()
form.set_input({"login": user, "password": pw})
print(form.print_summary())
browser["logonId"] = user
browser["logonPassword"] = pw
# print summary for review
resp = browser.submit_selected()
print('login submitted')
print(browser.url)
#click the invoices page
click_invoice_link = browser.open("https://www.sherwin-williams.com/InvoiceListView?catalogId=11052&storeId=10151&langId=-1")
csv_href = 'https://www.sherwin-williams.com/InvoiceListView?d-1337519-e=1&catalogId=11052&6578706f7274=1&storeId=10151&langId=-1'
page = browser.page
messages = page.find("span", class_="export csv")
resp = browser.session.get(csv_href)
resp.raise_for_status() # raise an exception for 404, etc.
surface = ''
desktop = ''
if pathlib.Path(surface).exists() :
#if surface condition true:
print('Found surface path')
save_path = surface
completeName = os.path.join(save_path, today + ".csv")
with open(completeName, 'wb') as outf:
outf.write(resp.content)
elif pathlib.Path(desktop).exists() :
#if surface condition true:
print('Found desktop path')
save_path = desktop
completeName = os.path.join(save_path, today + ".csv")
with open(completeName, 'wb') as outf:
outf.write(resp.content)
我已经修改了脚本,但是一旦我在正确的页面上并下载我的发票,我就能够按照 csv_href 链接进行操作。我目前无法选择正确的 html 字段来输入我的登录凭据。有人可以帮我弄清楚如何使用机械汤正确填写登录字段并通过登录页面吗?
解决方案
推荐阅读
- r - 如何从数据框中删除 NA 以在数据框上使用 sapply
- javascript - 无法使用 crypto.createDecipheriv() 正确解密文件。文件大小为 0 字节
- java - JBoss EAP 不在指定数据库上生成表
- gams-math - 如何在 GAMS 中表达条件?
- model-view-controller - 我如何在 MVC 网格删除控制器中解决我的这个问题
- android - 错误:无法解析“:app@debugAndroidTest/compileClasspath”的依赖关系:无法解析com.github.PhilJay:MPAndroidChart:v2.2.4
- visual-studio-code - 在对 vscode 扩展进行集成测试期间打开工作区
- python - pyqt 通过鼠标单击在主窗口中关闭自定义对话框
- php - 从 Word (.docx) 或 HTM 文件中读取数据并使用 PHP 上传到 mysql DB
- exception - 如何在 Flutter 中传递(在方法堆栈中)异常?