extracting paragraph in python using lxml

岁酱吖の 提交于 2019-12-23 02:45:11

问题


I would like to extract paragraphs in html by python. I used lxml module but it doesn't do exactly what I am looking for.

print html.parse(url).xpath('//p')[1].text_content()

<span id="midArticle_1"></span><p>Here is the First Paragraph.</p><span id="midArticle_2"></span><p>Here is the second Paragraph.</p><span id="midArticle_3"></span><p>Paragraph Three."</p>

I should add that, in different pages I have different number of paragraph, so would like to make a list and put paragraph into it after that.


回答1:


print html.parse(url).xpath('//p/text()')

Output

['Here is the First Paragraph.', 'Here is the second Paragraph.', 
 'Paragraph Three."']


来源:https://stackoverflow.com/questions/5034351/extracting-paragraph-in-python-using-lxml

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