阅读Python中beutilfulsoup的新行文本

2024-09-27 23:17:22 发布

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

当我运行命令时:

for row1 in soup.find_all('p',class_ = " cons mainText truncateThis wrapToggleStr"):
      print row1

输出:

enter image description here

当我运行命令时:

^{pr2}$

输出:

At Hyderabad, IDC you have to stay late most of the time if you have to get work done on time....
No growthMostly Verification workNot so good top management

我想要这样:

At Hyderabad, IDC you have to stay late most of the time if you have to get work done on time....

No growth

Mostly Verification work

Not so good top management

请帮我用beauthoulsoup得到我想要的输出。在


Tags: oftheto命令youmosttimehave
1条回答
网友
1楼 · 发布于 2024-09-27 23:17:22

你可以试试这个:

row1 = soup.find_all(attrs={"class_" : " cons mainText truncateThis wrapToggleStr"})
print row1[0].text #print the first p text
for text in [ x for x in row1[1].contents if getattr(x, 'name', None) != 'br']:
    print text  #print the second p text

输出:

At Hyderabad, IDC you have to stay late most of the time if you have to get work done on time

No growth

Mostly Verification work

Not so good top management

详细信息:

row1[1].contents将为您提供:

^{pr2}$

然后可以使用条件getattr(x, 'name', None) != 'br'过滤出文本:

[u'No growth', u'Mostly Verification work', u'Not so good top management']

相关问题 更多 >

    热门问题