python - 没有捕捉到 BeautifulSoup 所需的线条
问题描述
我正在使用 BeautifulSoup 从 URL https://www.champlain.edu/current-students搜索“学生登录”类。然后,我想在该类中进一步搜索,如果它包含字符串“用户名”或“密码”,则返回完整的行。我的工作代码返回类中的所有内容,但我没有运气增强它以获取仅包含“用户名”或“密码”的特定行。我已经包含了我当前输出的屏幕截图。任何指导将不胜感激。谢谢!!
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('https://www.champlain.edu/current-students')
bs = BeautifulSoup(html.read(), 'html.parser')
soup = bs.find(class_='student-login')
print(soup)
解决方案
这应该为您提供输入字段:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('https://www.champlain.edu/current-students')
bs = BeautifulSoup(html.read(), 'html.parser')
print(bs.select_one('#login-username'))
print(bs.select_one('#login-password'))
这是使用 CSS 选择器,#
前面的意思是您正在选择任何具有 id = login-username 的元素,我认为这是您想要的。
推荐阅读
- c# - 是否可以为 UWP 项目使用新的 SDK 样式的 .csproj 文件?
- c++ - 如何正确使用 dynamic_cast 向下转换?
- powershell - Powershell 输出到共享
- postgresql - 物化视图的调度程序 Postgresql + Redshift
- localhost - 通过 localhost 加载文件的最简单方法是什么?
- machine-learning - 安德鲁练习的机器学习课程可以在本地运行但无法提交?
- c++ - AWS C++ SDK,如何使用 SES 发送自定义电子邮件?
- javascript - 处理来自映射数组的 React Child
- javascript - 如何使用 Jquery 为 firefox 点击事件编写代码
- json - json 2 typescript 映射给出类型错误