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