from lxml import etree def get_mid(html): selector = etree.HTML(html) mid = selector.xpath('//div[@class="card-wrap"]') mid_str = [] for i in mid: temp = i.get("mid") if temp: mid_str.append(temp) return mid_str