gpt4 book ai didi

python - 使用 BeautifulSoup (python) 提取自定义 "data"标签

转载 作者:行者123 更新时间:2023-12-01 09:21:15 25 4
gpt4 key购买 nike

抓取如下所示的 HTML:

<div class="resultRow" data-unix="1528542937" id="resultRow1">
<div class="resultRow" data-unix="1528542937" id="resultRow2">
<div class="resultRow" data-unix="1528542937" id="resultRow1">

如何提取 data-unix 字段的值?

我是否必须使用 REGEXP 来实现此目的,还是有更好的方法?

for tmp in soup.findAll('div', {'class':'resultRow'}):
x = tmp.find(re.compile('/data-unix="(.*)"/'))

提前谢谢您。

最佳答案

根据您关于在循环内移动 Ajax1234 的答案的问题:

from bs4 import BeautifulSoup

s = """
<div class="resultRow" data-unix="1528542937" id="resultRow1">
<div class="resultRow" data-unix="1528542937" id="resultRow2">
<div class="resultRow" data-unix="1528542937" id="resultRow1">
"""

soup = BeautifulSoup(s, 'lxml')

final_results = []

for tmp in soup.find_all('div', {'class':'resultRow'}):

final_results.append(tmp['data-unix'])

print final_results

['1528542937', '1528542937', '1528542937']

关于python - 使用 BeautifulSoup (python) 提取自定义 "data"标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50777076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com