首页 文章

Python 2.7美丽的汤Img Src Extract

提问于
浏览
17
for imgsrc in Soup.findAll('img', {'class': 'sizedProdImage'}):
    if imgsrc:
        imgsrc = imgsrc
    else:
        imgsrc = "ERROR"

patImgSrc = re.compile('src="(.*)".*/>')
findPatImgSrc = re.findall(patImgSrc, imgsrc)

print findPatImgSrc

'''
<img height="72" name="proimg" id="image" class="sizedProdImage" src="http://imagelocation" />

这就是我想从中提取的内容,我得到了:

findimgsrcPat = re.findall(imgsrcPat, imgsrc)
File "C:\Python27\lib\re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

“””

4 回答

  • 33

    你将beautifulsoup节点传递给re.findall . 你必须将它转换为字符串 . 尝试:

    findPatImgSrc = re.findall(patImgSrc, str(imgsrc))
    

    更好的是,使用beautifulsoup提供的工具:

    [x['src'] for x in soup.findAll('img', {'class': 'sizedProdImage'})]
    

    给出了类'sizedProdImage'的img标签的所有src属性的列表 .

  • 0

    有更简单的解决方案:

    soup.find('img')['src']
    
  • 0

    你正在创建一个 re 对象,然后将其传递给 re.findall ,它需要一个字符串作为第一个参数:

    patImgSrc = re.compile('src="(.*)".*/>')
    findPatImgSrc = re.findall(patImgSrc, imgsrc)
    

    而是使用刚刚创建的patImgSrc对象的 .findall 方法:

    patImgSrc = re.compile('src="(.*)".*/>')
    findPatImgSrc = patImgSrc.findall(imgsrc)
    
  • 29

    在我的示例中,htmlText包含img标记,但它也可以用于URL . 看我的回答here

    from BeautifulSoup import BeautifulSoup as BSHTML
    htmlText = """<img src="https://src1.com/" <img src="https://src2.com/" /> """
    soup = BSHTML(htmlText)
    images = soup.findAll('img')
    for image in images:
        print image['src']
    

相关问题