BeautifulSoup: Extract img alt data

自闭症网瘾萝莉.ら 提交于 2019-12-18 05:12:27

问题


I have following image html and I am trying to parse information that is in alt. Currently I am able to successfully extract images.

html (What I currently parse

<img class="rslp-p" alt="Sony Cyber-shot DSC-W570 16.1 MP Digital Camera - Silver" src="http://i.ebayimg.com/00/$(KGrHqZ,!j!E5dyh0jTpBO(3yE7Wg!~~_26.JPG?set_id=89040003C1" itemprop="image" />

I construct the image name from what I parse:

Current Code

def main(url, output_folder="~/images"):
         """Download the images at url"""
         soup = bs(urlopen(url))
         parsed = list(urlparse.urlparse(url))
         count = 0
         for image in soup.findAll("img"):
             print image
             count += 1
             print count
             print "Image: %(src)s" % image
             image_url = urlparse.urljoin(url, image['src'])
             filename = image["src"].split("/")[-1].split("?")[0].replace("$",'').replace(".JPG",".jpg").replace("~~_26",str(count)).lstrip("(")
             parsed[2] = image["src"]
             outpath = os.path.join(output_folder, filename)
             urlretrieve(image_url, outpath)

What I would like to do is extract is

alt="Sony Cyber-shot DSC-W570 16.1 MP Digital Camera - Silver"

also I want to use alt data as the file name when I extract the image.


回答1:


Inside your for loop, you can obtain that by simply doing

image.get('alt', '')

This is explained in BeautifulSoup's documentation ("The attributes of Tags").



来源:https://stackoverflow.com/questions/11696745/beautifulsoup-extract-img-alt-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!