Search code examples
pythonhtmlpython-3.xpython-requests-html

Requests-HTML scrape <a> tag image url (Requests-HTML, python)


html to attempt to extract the cpu image from the following webpage i have identified that the image url is in a tag with the class name item: Chrome inspect tool

Here is my code

from requests_html import HTMLSession
session = HTMLSession()

r = session.get('https://au.pcpartpicker.com/product/jLF48d')

about = r.html.find('.item')

print(about)

This prints

Element 'a' class=('item',) onclick='show_gallery(0, carousel_images);return false;'

However when I change the print statement to:

print(about.absolute_links)

I get the following error:

AttributeError: 'list' object has no attribute 'absolute_links'

Any idea why this is happening and how i can fix it?

If you require any more information please let me know.

Thanks


Solution

  • r.html.find('.item') returns a list and list has no attribute absolute_links. Since there may be not only one node can be found with .item, find() method gives you a list as expected.

    It will be convinient to get a single node with

    about = r.html.find('.item')[0]
    

    However, this won't give you the img link by about.absolute_links , because the element found here is of <a>, not <img>

    about = r.html.find('.item')[0]
    img = about.xpath('//img')[0]
    img.attrs['src'] # => '//cdn.pcpartpicker.com/static/forever/images/product/55aea2dd64e2e3a3e3b1d678048d8d76.256p.jpg'