When we use XPath to crawl web pages, we may encounter such a problem: the list printed using the text method is empty, which?can be caused by non specified direct content.
attention:text() method can only get direct content, string(.) can get whole content and then?composed?in?one?whole?string?returned?to?the?caller.
solution:
<div class="bottom">
<ul class="unstyled">
<li><a href="monthdata.php?city=北京">北京</a></li>
<li><a href="monthdata.php?city=上海">上海</a></li>
<li><a href="monthdata.php?city=广州">广州</a></li>
<li><a href="monthdata.php?city=深圳">深圳</a></li>
<li><a href="monthdata.php?city=杭州">杭州</a></li>
<li><a href="monthdata.php?city=天津">天津</a></li>
<li><a href="monthdata.php?city=成都">成都</a></li>
<li><a href="monthdata.php?city=南京">南京</a></li>
<li><a href="monthdata.php?city=西安">西安</a></li>
<li><a href="monthdata.php?city=武汉">武汉</a></li>
</ul>
</div>
# 第一种方法
li_list = tree.xpath('//div[@class="bottom"]/ul/li/a/text()')
# 第二种方法
li_list = tree.xpath('//div[@class="bottom"]/ul/li//text()')
|