python - I need help extracting a specific piece of text from a webpage
问题描述
I am trying to assign the 11101973 number in this html file to a variable but need a way of grabbing that number only without any of the extra information :
<div class="chooseSizeContainer" id="2SizeContainer" style="display:none;">
<div class="chooseSizeLinkContainer active">
<a id="US-13" href="javascript:void(0);"
class="chooseSizeLink chooseSizeLinkActive"
data-size="13"
onclick="ProductDetails.changeSizeAffectedLinks(
'11101973',
'£ 135.95',
'£ 135.95',
'0',
'£ 0.00saved!',
'13',
'13',
'15',
'false',
'false',
'false',
'false',
'unknown',
'US-',
'555088-015');">13</a>
</div>
The page source is here if more info is needed : view-source:https://www.kickz.com/uk/jordan-basketball-retro-air-jordan-1-retro-high-og-black_varsity_red_sail_university_blue-107840036 Any help appreciated!
解决方案
beautifulsoup 用于解析 html 元素而不是 javascript 变量。那里很少有 javascript 解析器,但对于简单的任务,我更喜欢Regex
import requests, re
page = requests.get(url).text
theNumber = re.search(r'collectAskInput\((\d+)).group(1)
print(theNumber)
# 11101973
它搜索号码
onclick="return ProductDetails.collectAskInput(11101973)
推荐阅读
- python - 'style.theme_create' python 的问题
- applescript - 在AppleScript中从列表中选择的枚举关联数组
- python - Cloud Scheduler 调用的 GCP Cloud Run 应用的当前最大超时是多少
- python - 使用 namedtuple 使用 Python 将 txt 转换为 JSON
- regex - Azure 数据资源管理器,Kusto:替换正则表达式问题
- vue.js - vue-recaptcha 增加过期时间
- android - 通过 termux 命令行启动任何应用程序,例如 chrome、youtube 和其他一些应用程序,如 cryptotabbrowser
- mysql - mysql在午夜不工作之间
- influxdb - 查询 InfluxDB 最后一点
- java - 在 Wildfly 中部署 Spring 应用程序时出现 Weld 错误