1

I am using a code to scrape a PDF to generate a relevant dictionary. My code works when I access each text block individually, i.e

x = scraperwiki.pdftoxml(u.read())
    r = lxml.etree.fromstring(x)
    s = r.xpath('//page[@number="142"]/text[@left = "134"]')
    print s[8].text 

print s[0],s[1].. all seem to work but when I try the same for

x = scraperwiki.pdftoxml(u.read())
    r = lxml.etree.fromstring(x)
    s = r.xpath('//page[@number="142"]/text[@left = "134"]')
    print s[0:8].text

I get this error: AttributeError: 'list' object has no attribute 'text'

Can anyone tell me what's wrong?

1
  • you could just add /text() to the end of your xpath expression if all you care about is the text from each node. Commented Aug 30, 2014 at 14:28

1 Answer 1

1

text is an attribute of each element, not of the list.

Iterate each elements.

x = scraperwiki.pdftoxml(u.read())
r = lxml.etree.fromstring(x)
s = r.xpath('//page[@number="142"]/text[@left = "134"]')
for elem in s[:8]:
    print elem.text

or use list comprehension:

x = scraperwiki.pdftoxml(u.read())
r = lxml.etree.fromstring(x)
s = r.xpath('//page[@number="142"]/text[@left = "134"]')
print [elem.text for elem in s[:8]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.