daobalong
daobalong
2020-09-01 20:40

求助:python爬取图片,怎么取出标签里的src内容?

30
  • python
from bs4 import BeautifulSoup
import requests
import os
import lxml
import parsel


headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}

url = 'https://www.archdaily.com/946565/higashi-sanchome-toilet-nao-tamura/5f4790beb35765c1ca0000ea-higashi-sanchome-toilet-nao-tamura-photo'


result = requests.get(url, headers= headers)
parse = parsel.Selector(result.text)
targetCode = parse.xpath('/html/body/div[1]/div[1]/div[1]/div/div/div[1]/figure/img')
print(targetCode)

返回的结果

[<Selector xpath='/html/body/div[1]/div[1]/div[1]/div/div/div[1]/figure/img' data='<img class="afd-gal-img js-gal-img" i...'>]

我需要的内容是img标签里的src或是data-largesrc图片地址(两者内容好像一样),试过print(targetCode.extract_first().encode('utf-8'))
但是返回img内容只有一半

b'<img class="afd-gal-img js-gal-img" id="5f4790beb35765c1ca0000ea-higashi-sanchome-toilet-nao-tamura-photo" alt="Higashi Sanchome Toilet / Nao Tamura,\xc2\xa9 Satoshi Nagare, Courtesy of The Nippon Foundation" style="transform-origin: 50% 50%;">'

前面有个b不知道是什么东西,然后也不是我要的,用.extract()也不行,折腾好久了,麻烦帮忙解答下,谢谢
图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答