使用LXML进行Web抓取

时间:2019-07-01 02:51:44

标签: python web-scraping lxml

如何使用xpath提取标签之间的文本?例如,我尝试提取以“ Area:”开头的文本,下面的代码仅提取单词“ Area”,而不提取以下文本。

tree = lxml.html.fromstring(response.text)

xpath_ex= '//b[contains(text(),"Area:")]/descendant::text()'

raw_ex = tree.xpath(xpath_ex)

1 个答案:

答案 0 :(得分:0)

您评论中发布的html不完整,但假设它看起来像这样:

 resp = """
    <div class="text"><h4>ABC, Assistant Professor&nbsp;</h4> 
      <p><b>Area:</b>&nbsp;Natural Language Processing, Artificial Intelligence, 
Computer Graphics, Computer Vision<a href=" somelink/people/Faculty/Profile/ABC.html"></a> </p> <p>  <a href="/computing/people/faculty/ABC.html">Profile &amp; Contact Information&nbsp;</a>  &nbsp;| &nbsp;Home Page</p> 
    </div>
"""

尝试一下:

from lxml.html import fromstring
tree = fromstring(resp)

xpath_ex= tree.xpath('//div[@class="text"]/p')
print(xpath_ex[0].text_content())

输出:

  

领域:自然语言处理,人工智能,计算机图形学,计算机视觉