1

I am trying to extract links of .jpg images from the following link: https://asheville.craigslist.org/search/sss

If you look nested in the nodes, there are nodes with the links I need to extract.

screenshot of referenced html

I am new to scrapy and xpath and I can't seem to get anything to return other than an empty list.

I've tried many varieties of this code without any luck:

response.xpath('//*[@id="sortable-results"]/ul/li/a/img/')
Keenan Burke-Pitts
  • 475
  • 2
  • 6
  • 17

2 Answers2

0

It seems like the data is hidden in <a> nodes data-ids attribute and later unpacked by javascript into a gallery of images.

<a href="/cto/6095960745.html" class="result-image gallery" 
data-ids="1:01414_7WJQELsYuex,1:00t0t_kxF99J8uXmP,1:00S0S_dgnLA6FvDKX,1:00404_kTP1mB2Flpb,1:00P0P_j5On1SCHLuP,1:00a0a_jZYNazvdTgo,1:00Y0Y_9HJf6UJJVg7,1:00p0p_loCrLMXpS5s,1:00k0k_3e296xxBfXi,1:00f0f_5QpRYaBnIK7,1:00e0e_aZTOihYtz9C,1:00c0c_iatoB70CmWg,1:00X0X_dwt0ZbxYJNC,1:00k0k_k3dPBZpN9KM,1:00W0W_f51jQcPO86R">\n
<span class="result-price">$1700</span>\n        </a>

We can reverse engineer this by extracting the ids and then formatting our own image urls:

ids = response.xpath("//a[@class='result-image gallery']/@data-ids").extract()
ids = ''.join(ids).split(',')  # all of ids are separeted by comma
template = "https://images.craigslist.org/{}_300x300.jpg"
for img_id in ids:
    # e.g. 1:00G0G_anZn4IdI4pK'
    # we want to get rid of 1: part
    img_id = img_id.split(':')[-1] 
    url = template.format(image id)
    print(url)
Granitosaurus
  • 20,530
  • 5
  • 57
  • 82
0

Try to implement below XPath expression to get image source links:

//div[@id="sortable-results"]//img/@src
Andersson
  • 51,635
  • 17
  • 77
  • 129