迭代结果集bs4

2024-04-29 15:49:33 发布

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

我用bs4来提取bs4中的这个结果集。在

<div> 
<div>
</div>
Content 1
</div>

<div>
Content 2
</div>

我试图提取这两个元素。在

Moi not cute not hot, the ugly bui bui type 1和{}

^{pr2}$

这是我的密码。但是,如何遍历结果集,使其仅在结束div之前提取内容

letters.find_all('div')返回一个空集。在


Tags: thediv元素密码内容cutetypenot
1条回答
网友
1楼 · 发布于 2024-04-29 15:49:33

所有信息:

from bs4 import BeautifulSoup
import urllib
import re

r = urllib.urlopen(
    'http://forums.hardwarezone.com.sg/eat-drink-man-woman-16/%5Bofficial%5D-chit-chat-students-part-2-a-5526993-55.html').read()

soup = BeautifulSoup(r, "lxml")
letters = soup.find_all("div", attrs={"id":re.compile("post_message_\d+")})
for a in letters:
    print [b.strip() for b in a.text.strip().split('\n') if b.strip()]

相关问题 更多 >