Split string from BeautifulSoup output in a list

隐身守侯 提交于 2021-01-28 09:35:28

问题


I have the following output from my code

Code: text = soup.get_text()

Output:

Article Title

    Some text: Text blurb.

More blurb.

Even more blurb. 

Some more blurb. 





Second Article Title

Some text: Text blurb.

More blurb.

Even more blurb. 

Some more blurb. 

Next, when I do test = text.splitlines(), the output changes to

u'Article Title', u'', u'Some text',u'Text blurb',u'More blurb',u'Even more blurb',u'Some more blurb',, u'', u'', u'', u'', u'',u'Second Article Title', u'', u'Some text:',u'Text blurb',u'More blurb',u'Even more blurb',u'Some more blurb',, u'', u'', u'', u'', u'',

I'd like to split the string using u'', u'', u'', u'', u'' so that I can then individually parse out the lines. I'd have liked to use the tags but their structure makes it difficult to use.

How do I perform the split? I have tried:

result = [list(g) for k,g in groupby(test,lambda x:x=="u''") if not k]
print result

and

for item in test:
    arr = re.split("u'', u'', u'', u'', u''",item, flags=re.UNICODE)
    print arr

but they don't give me the desired output.


回答1:


If you take a look at your text, you want to split by repeated newlines \n from

text
>> 'Article Title\n\n    Some text: Text blurb.\n\nMore blurb.\n\nEven more blurb. \n\nSome more blurb. \n\n\n\n\n\nSecond Article Title\n\nSome text: Text blurb.\n\nMore blurb.\n\nEven more blurb. \n\nSome more blurb. '

You can then just use define a parameter for text.split('\n\n\n\n\n'), if you don't add a parameter, Python simply splits by whitespaces. After your first split, you can then split your other elements by \n\n.

[i.split('\n\n') for i in text.split('\n\n\n\n\n')]

>>[['Article Title',
  '    Some text: Text blurb.',
  'More blurb.',
  'Even more blurb. ',
  'Some more blurb. '],
 ['\nSecond Article Title',
  'Some text: Text blurb.',
  'More blurb.',
  'Even more blurb. ',
  'Some more blurb. ']]


来源:https://stackoverflow.com/questions/53586149/split-string-from-beautifulsoup-output-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!