losee losee - 10 months ago 36
Python Question

I am getting the error 'Response' object has no attribute 'fromstring'

I am trying to use xpath in my django app and I keep getting the error

'Response' object has no attribute 'fromstring'

I don't understand why. I have researched and the only thing I saw was people having probblems with text instead of from string. Heres my code

def panties():
from lxml import html
pan_url = 'http://www.panvideos.com'
html = requests.get(pan_url, headers=headers)
soup = BeautifulSoup(html.text, 'html5lib')
video_row = soup.find_all('div', {'class': 'video'})

def youtube_link(url):
youtube_page = requests.get(url, headers=headers)

tree = html.fromstring(youtube_page.content)

the_link = tree.xpath('//*[@id="wrapper"]/div[1]/div[2]/div[3]/div[1]/div[1]/h1')

return the_link

entries = [{'text': div.h4.text,
'href': div.a.get('href'),
'tube': youtube_link(div.a.get('href')),
} for div in video_row][:1]

return entries

any help would be great.

I'm following the hitchhikers guide to python but while I search for answers I' keep seeing people use etree and not the way he's using it


Don't nest functions like this. Un-nest your function and it will work fine.

You define html = requests.get(*)

What this returns is a response object.

In your nested function you're using this html instead of what you imported from lxml import html because of the namespace(s).