我尝试使用BeautifulSoup从输入下拉列表中从Web抓取数据
这是价值下降
<selected name="try">
<option value="G1">1</option>
<option value="G2">2</option>
</selected>
我这样尝试
soup = BeautifulSoup(url, 'html.parser')
soup['selected'] = 'G1'
data = soup.findAll("table", {"style": "font-size:14px"})
print(data)
每次提交下拉菜单时,它将使用带有<table>
标签的数据
但是它仅在主页上显示<table>
,如何从每个下拉列表中获取数据?
答案 0 :(得分:1)
尝试使用attribute CSS选择器
soup.select('option[value]')
[]
是一个属性选择器。这将查找具有option
属性的value
标签元素。如果可以使用父类/ id,则在页面上有更多下拉菜单时会很有用。
items = soup.select('option[value]')
values = [item.get('value') for item in items]
textValues = [item.text for item in items]
使用父项name
属性将下拉列表限制为一个(希望-您需要测试并查看是否需要进一步的操作才能充分限制)。与descendant combinator一起使用:
items = soup.select('[name=try] option[value]')
答案 1 :(得分:1)
您仍然继续使用class ChatViewController: UIViewController, UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PCRoomDelegate {
var navbarView: UIView!
var navbarAvatar: UIImageView!
var navbar: UILabel!
var status: String? {
didSet {
self.updateNavText()
}
var username: String? {
didSet {
self.updateNavText()
}
var displayName: String?
didSet {
self.updateNavText()
}
var statusText: NSAttributedString {
let text = NSMutableAttributedString(string: displayName ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
if let username = self.username {
text.append("\n@\(username)")
}
if let status = self.status {
text.append("\n\(status)")
}
return text
}
override func viewDidLoad() {
super.viewDidLoad()
self.navbarView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4, height: 44.0))
self.navbarView.backgroundColor = UIColor.clear
self.navbar = UILabel(frame: CGRect(x: 44.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4 - 44, height: 44.0))
self.navbar.backgroundColor = UIColor.clear
self.navbar.numberOfLines = 2
self.navbar.textAlignment = NSTextAlignment.left
let bodyText = NSMutableAttributedString(string: "Halil İbrahim YÜCE", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
self.updateNavText()
self.navbarView.addSubview(navbar)
self.navigationItem.titleView = self.navbarView
}
func onPresenceChanged(
stateChange: PCPresenceStateChange,
user: PCUser
) {
print("User \(user.displayName)'s presence changed to \(stateChange.current.rawValue)")
DispatchQueue.main.async {
self.status = stateChange.current.rawValue
}
}
func updateNavText() {
self.navbar.attributedText = self.statusText
}
和findAll()
来完成工作。
find()