0

这是我要抓取的网站的 HTML 代码:

<div id="quranOutput">
  <a class="key" name="1:1"></a>
    <div class="verse ayahBox1" id="verse_1">

这是我在动态 django scraper 中使用的 xpath,但它不起作用:

//div[@class="ayah language_6 text"]/a/@name

有人可以帮我找出检索名称的正确方法,即(name="1:1")。

4

1 回答 1

1

使用 xpath:

//div[@id="quranOutput"]/a[@class="key"]/@name

>>> import lxml.html
>>> 
>>> root = lxml.html.fromstring('''
... <html>
...     <body>
...         <div id="quranOutput">
...             <a class="key" name="1:1"></a>
...             <div class="verse ayahBox1" id="verse_1"></div>
...         </div>
...     </body>
... </html>''')
>>> 
>>> print root.xpath('//div[@id="quranOutput"]/a[@class="key"]/@name')
['1:1']
于 2013-07-17T03:48:07.813 回答