如何使用BeautifulSoup&Python在子列表中为父列表中的选择子集获取选项?

2024-10-03 11:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用BeautifulSoup来刮取一个包含两个HTML选择列表的“前缀”和“后缀”。第二个列表(后缀)仅在用户从“父”前缀选择列表中选择一个选项时显示。后缀选择列表中显示的选项随前缀列表中的每个选择而变化。在

我正在获取一个特定前缀的子集,我想为每个前缀子集的后缀选项获取文本。在

Python:

prefixSubset = ['A', 'B', 'C']
url = 'http:myUrl.com'
html = urlopen(url)
soup = BeautifulSoup(html)
scrapedPrefixesSubset = soup.findAll(value = prefixSubset)

# Now I want to iterate over scrapedPrefixSubset and get the text for all
# suffix options, which change for each option selected in the prefix list

父列表中选择了“B”的HTML:

^{pr2}$

有没有直截了当的方法?在


Tags: the用户文本httpurl列表forhtml