【问题标题】:can't get value inside <a> tag in lxml无法在 lxml 中的 <a> 标记内获取值
【发布时间】:2017-04-20 17:10:16
【问题描述】:

我正在使用 lxml 从网站上抓取数据。 html代码sn-p是

<span class="pro-contact-text">
<a class="click-to-call-link text-gray-light trackMe" href="javascript:;" 
   objId="104809" compid="clickToCall_profile_organic" phone="(617) 505-4149"">Click to Call</a>
</span>

我可以使用 xpath(例如 //*(some tags)/span[@class="pro-contact-text"] )到达跨度,当我打印变量时,它会打印一个有效元素(例如 &lt;Element span at 0x3589510&gt; ) 当我将 xpath 扩展到 { span[@class="pro-contact-text"]/a/@phone } 时,它返回一个空列表。 有人可以帮我做这件事吗?

【问题讨论】:

  • 你的意思是你使用//*(some tags)/span[@class="pro-contact-text"]/a/@phone还是只使用span[@class="pro-contact-text"]/a/@phone
  • **一些标签 - 基本上我只是添加 /a/@phone 到它
  • 你能分享你使用的确切表达吗?无需替换**some tags
  • //*[@id="profileHeader"]/div/div/div/div[5]/div/ul/li[@class="sidebar-item pull-right profile-content-narrow"]/div/span[@class="pro-contact-text"]/a/@phone

标签: html xpath web-scraping lxml lxml.html


【解决方案1】:

问题在于无效的 html。

属性phone"" 结尾(两个引号)。

phone="(617) 505-4149"">
                      ^

【讨论】:

  • 有没有办法检索数据?
猜你喜欢
  • 2020-11-19
  • 2013-03-15
  • 1970-01-01
  • 2021-05-23
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
相关资源
最近更新 更多