提取页面介绍信息与美丽的汤

2024-09-27 00:17:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的美丽汤,我试图提取的信息,出现在一个页面上。这个信息包含在div^{cl1}$

table = soup.findAll('div', {'class': '_50f3'})

[<div class="_50f3">Lives in <a class="profileLink" data-hovercard="/ajax/hovercard/page.php?id=114148045261892" href="/Fort-Worth-Texas/114148045261892?ref=br_rs">Fort Worth, Texas</a></div>,
 <div class="_50f3">From <a class="profileLink" data-hovercard="/ajax/hovercard/page.php?id=111762725508574" href="/Dallas-Texas/111762725508574?ref=br_rs">Dallas, Texas</a></div>]

例如,在上面我想存储“Lives in”:“Fort Worth,Texas”和“From”:“Dallas,Texas”。但在最一般的情况下,我想存储任何信息有在那里。你知道吗

非常感谢您的帮助!你知道吗


Tags: indiv信息datapageajaxclassphp
2条回答

在一般情况下,这只是您需要的^{}—它将通过子节点递归地构造一个元素文本字符串:

table = soup.find_all('div', {'class': '_50f3'})
print([item.get_text(strip=True) for item in table])

但是,也可以分别提取标签和值:

d = {}
for item in table:
    label = item.find(text=True)
    value = label.next_sibling

    d[label.strip()] = value.get_text()

print(d)

印刷品:

{'From': 'Dallas, Texas', 'Lives in': 'Fort Worth, Texas'}
for i in range(len(table)):
    print(table[i].text)

应该有用

相关问题 更多 >

    热门问题