我是 python 新手,我正在尝试使用它来自动化工作中的手动表单填写任务。
我正在利用mechanicalsoup 功能来浏览网站并使用csv 文件中的值填写相同的表格。我可以确认下面的代码适用于我的 csv 中的第一行值,但在那之后会中断。我的 csv 文件有三列。
我真的很感激这方面的任何帮助。谢谢你。
这是我的代码:
#login
import mechanicalsoup
browser = mechanicalsoup.StatefulBrowser()
browser.open("<some website url>")
browser.select_form('form[action="/logins"]')
browser["username"] = "***"
browser["password"] = "*****"
browser.submit_selected()
#navigate to form and fill
def myfunction(esp, supnum, mid):
url_esp = "<some website url>" + esp
browser.open(url_esp)
browser.follow_link("<some link>")
url_facility = browser.get_url()
url_facility_edit = url_facility + "<some link>"
browser.open(url_facility_edit)
browser.select_form('form[id="edit_facility"]')
browser["facility[oracle_identifier]"] = supnum
browser["facility[ffc_identifier]"] = mid
response = browser.submit_selected()
#import values from csv to pass into function
import csv
with open('biweekly_oracle_esp_test.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
for row in reader:
esp = row[0]
supnum = row[1]
mid = row[2]
myfunction(esp, supnum, mid)
这是错误:
Traceback (most recent call last): File "open_file_test2.py", line 32, in <module> myfunction(esp, supnum, mid) File "open_file_test2.py", line 19, in myfunction response = browser.submit_selected() File "C:\Python\lib\site-packages\mechanicalsoup\stateful_browser.py", line 232, in submit_selected *args, **kwargs) File "C:\Python\lib\site-packages\mechanicalsoup\browser.py", line 213, in submit response = self._request(form, url, **kwargs) File "C:\Python\lib\site-packages\mechanicalsoup\browser.py", line 155, in _request data.setdefault(name, []).append(value) AttributeError: 'str' object has no attribute 'append'