gpt4 book ai didi

python - 是否可以根据字段类型(例如 TextControl、TextareaControl)来选择表单字段?

转载 作者:太空宇宙 更新时间:2023-11-03 19:36:57 25 4
gpt4 key购买 nike

我正在尝试使用 mechanize 解析 html 表单。表单本身有任意数量的隐藏字段,并且字段名称和 ID 是随机生成的,因此我没有明显的方法来直接选择它们。显然,使用名称或 ID 已经过时了,并且由于隐藏字段的随机数量,我无法根据序列号选择它们,因为序列号也总是会发生变化。

但是,总是有两个紧邻的 TextControl 字段,然后在其下方是一个 TextareaControl。这些是我也需要访问的 3 个字段,基本上我需要解析它们的名称,一切都很好。在过去的几个小时里,我一直在浏览 Mechanize 文档,但没有想出任何似乎能够做到这一点的东西,无论它看起来多么简单(无论如何对我来说)。

我提出了一个替代解决方案,其中涉及创建表单控件列表,迭代它以查找包含字符串“Text”的控件,返回这些控件的新列表,然后最终使用删除名称一个正则表达式。虽然这有效,但似乎没有必要,我想知道是否有更优雅的解决方案。谢谢大家。

编辑:如果有人好奇的话,这就是我目前正在做的提取该信息的操作。我想我可能会坚持下去。这似乎没有必要,但它可以完成工作,而且不需要什么密集的工作,所以我不担心效率或其他任何事情。

def formtextFieldParse(browser):
'''Expects a mechanize.Browser object with a form already selected. Parses
through the fields returning a tuple of the name of those fields. There
SHOULD only be 3 fields. 2 text followed by 1 textarea corresponding to
Posting Title, Specific Location, and Posting Description'''
import re
pattern = '\(.*\)'
fields = str(browser).split('\n')
textfields = []
for field in fields:
if 'Text' in field: textfields.append(field)
titleFieldName = re.findall(pattern, textfields[0])[0][1:-2]
locationFieldName = re.findall(pattern, textfields[1])[0][1:-2]
descriptionFieldName = re.findall(pattern, textfields[2])[0][1:-2]

最佳答案

我认为 mechanize 没有您需要的确切功能;您可以使用 mechanize 获取 HTML 页面,然后使用 BeautifulSoup 解析后者吗?

关于python - 是否可以根据字段类型(例如 TextControl、TextareaControl)来选择表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3122687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com