从selenium下拉列表中获取所有组合,不带选项或选择标记

2024-05-14 23:16:42 发布

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

我正在与Selenium合作开发相应的网站:http://calstate-la.bncollege.com/webapp/wcs/stores/servlet/TBWizardView?catalogId=10001&langId=-1&storeId=30556。在

我在这个网站的目标是从他们各自的下拉菜单中获取所有可能的系、课程和部分的组合。我遇到的主要问题是我想不出从下拉菜单中获取值的任何方法。在

根据其他类似于我的堆栈溢出问题,他们提到了使用select标记和option标记的解决方案。但是,当我查看这个页面源代码时,下拉菜单没有这样的标记。在

所以我需要帮助,试图从下拉菜单中得到所有的组合,但我不知道如何在我的特殊情况下继续。我还想提到我在Python上工作。在


Tags: 标记comhttp网站seleniumlastoreswebapp
1条回答
网友
1楼 · 发布于 2024-05-14 23:16:42

实际上,我尝试过在这里使用selenium,但由于页面的异步特性和“人工”下拉列表(here is what I had so far),它确实会很快变得很痛苦。在

这里有一种使用^{}^{}(完全不需要浏览器)的替代方法。在

其目的是模拟填充下拉列表的udnerling请求:

from bs4 import BeautifulSoup
import requests

CATALOG = 10001
STORE = 30556

url = 'http://calstate-la.bncollege.com/webapp/wcs/stores/servlet/TBWizardView?catalogId={catalog}&langId=-1&storeId={store}'.format(catalog=CATALOG,
                                                                                                                                     store=STORE)
xhr_url = 'http://calstate-la.bncollege.com/webapp/wcs/stores/servlet/TextBookProcessDropdownsCmd'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36'}

session = requests.Session()
response = session.get(url, headers=headers)
soup = BeautifulSoup(response.content)

campus = soup.find('input', attrs={'name': 'campus1'}).get('value')
book_row = soup.find('div', class_='bookRowContainer')

params = {
    'campusId': campus,
    'deptId': '',
    'courseId': '',
    'sectionId': '',
    'storeId': STORE,
    'catalogId': CATALOG,
    'langId': '-1',
    'dropdown': 'term'
}

terms = book_row.select('li.termOption')
for term in terms:
    params['termId'] = term.get('data-optionvalue')
    response = session.post(xhr_url, params=params, headers=headers)
    print response.content

这将以JSON格式打印所有术语的所有部门。在

2014年秋季:

^{pr2}$

2014年夏季:

[
    {"categoryName":"AAAS","categoryId":"63007512","categoryIdentifier":"670_1_A14_4","title":"AAAS"},
    {"categoryName":"ACCT","categoryId":"63007490","categoryIdentifier":"670_1_A14_5","title":"ACCT"},
    ...
]

留下你的Course和{}部分作为家庭作业。在

希望有帮助。在

相关问题 更多 >

    热门问题