use*_*601 4 python xpath lxml python-2.7
仅使用Twitter作为示例而忽略了它们具有完全可用的API的事实,以下脚本从用户页面获取当前的第5条推文.
import urllib2
from lxml import etree
xpathselector = "/html/body/div/div[2]/div/div[5]/div[2]/div/ol/li[5]/div/div/p"
url = "https://twitter.com/bmthofficial"
response = urllib2.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
result = tree.xpath(xpathselector)
print result[0].text
Run Code Online (Sandbox Code Playgroud)
在这篇文章的时候它打印:
从下午2点30分,赢取阅读节门票,并介绍
现在,它打印<p> </ p>的内容,我将如何获取P的类名?它的HTML看起来像这样.
<p class="js-tweet-text tweet-text">From 2.30pm, win tickets to Reading Festival, and introduce <a dir="ltr" class="twitter-atreply pretty-link" href="/bmthofficial"><s>@</s><b>bmthofficial</b></a> onstage!</p>
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏!谢谢!
元素的使用get
方法:
print result[0].get('class')
Run Code Online (Sandbox Code Playgroud)
版画
js-tweet-text tweet-text
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3568 次 |
最近记录: |