【发布时间】:2021-08-07 18:44:09
【问题描述】:
我试图从这个链接中获取 6,550 英镑的基本价格:https://www.plates4less.co.uk/private-plate_o/CSG1S 它位于一个名为 BasePrice 的隐藏输入中,当我查看页面源时,'6550' 位于一个名为 value 的属性中:
<input type="hidden" name="BasePrice" id="BasePrice" value="6550">
我的代码是:
url = 'https://www.plates4less.co.uk/private-plate_o/CSG1S'
req = session.get(url, headers=headers)
bsObj = BeautifulSoup(req.text, features="html.parser")
price = bsObj.find("input", {"name" : {"BasePrice"}})
print(price['value'])
但它返回KeyError: 'value'
当我运行它时(即检索标签但没有尝试访问“值”:
url = 'https://www.plates4less.co.uk/private-plate_o/CSG1S'
req = session.get(url, headers=headers)
bsObj = BeautifulSoup(req.text, features="html.parser")
price = bsObj.find("input", {"name" : {"BasePrice"}})
print(price)
我明白了:
<input id="BasePrice" name="BasePrice" type="hidden"/>
为什么我注意到可以访问value 属性?
【问题讨论】:
标签: python-3.x web-scraping beautifulsoup