首页 > 解决方案 > 机械汤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 字段来输入我的登录凭据。有人可以帮我弄清楚如何使用机械汤正确填写登录字段并通过登录页面吗?

标签: pythonweb-scrapingmechanicalsoup

解决方案


推荐阅读