如下的一段html:

<h2class="Left-title"id="baseParamInfo">
<aclass="jishujishu2"target="_self"href="http://product.cnmo.com/1622/1621678/canshu.shtml">图文式<em></em></a>
<aclass="jishucur"target="_self"href="javascript:void(0);">列表式</a>
LGG4产品概要</h2>


想提取其中的“正文”:LGG4产品概要

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

使用的python代码如下:

fromlxml.html.soupparserimportfromstring

content=#crawlpageandreadhtml

root=fromstring(content)

desc_list=root.xpath('//*[@id="baseParamInfo"]')

title=desc_list[0].text#问题:获取的title为空字符串


有些线索:
代码中获取的desc_list为一个list,
只有两个元素,分别对应html中的“图文式”和“列表式”两段,
但就是获取不到“LGG4产品概要”这段信息。


请问,我该如何获取“LGG4产品概要”这段信息啊?

2 个解决方案

#1


下面的v是指desc_list[0].

help(v)显示:

引用
|tail
|Textafterthiselement'sendtag,butbeforethenextsibling
|element'sstarttag.ThisiseitherastringorthevalueNone,if
|therewasnotext.
|
|text
|Textbeforethefirstsubelement.Thisiseitherastringor
|thevalueNone,iftherewasnotext.

所以text是第一个<a>之前的text,当然是空的.

可以用下面的代码找出所有不在子节点中的text:


In[91]:printv.text+''.join([child.tailforchildinv])



LGG4产品概要

更多相关文章

  1. 如何在数组中存储产品数量
  2. (phpQuery)对网站产品信息采集代码的优化
  3. Mysql--可用的 MySQL 产品和专业服务
  4. 如何在产品和类别应用程序树中将1个表连接到(2个不同的表作为一个
  5. 为独立的“产品”打包django项目及其依赖项
  6. 软交换FreeSWITCH系统概要和源代码分析预备知识

随机推荐

  1. Android(安卓)Adapter适配器模板(笔记)
  2. android:gravity和android:layout_gravit
  3. Android 游戏设计教程
  4. android----UI组件
  5. Spinner的Android:prompt无法显示文本
  6. Android(安卓)Design Support Library(二)
  7. Application、Activity Stack 和 Task的
  8. Android:Gravity控制格式
  9. 关于progressbar进度条的显示风格及一些
  10. 源码解析Android中AsyncTask的工作原理