import requests from lxml import etree # User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36 def body(): url = "https://mp.weixin.qq.com/s/aPYBMeS9ZHY0q41xiv_Afw" header = {‘User-Agent‘:‘Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36‘} respones = requests.get(url,headers=header) # print(respones) respones.encoding = "utf_8" html = respones.text obj = etree.HTML(html) obj_1 = obj.xpath(‘//div[@class="rich_media_content "]/p/strong/img/@data-src‘) obj_2 = obj.xpath(‘//div[@class="rich_media_content "]/p/img/@data-src‘) obj_3 = obj.xpath(‘//div[@class="rich_media_content "]/p/span/strong/img/@data-src‘) obj_4 = obj.xpath(‘//div[@class="rich_media_content "]/p/span/strong/span/img/@data-src‘) obj_5 = obj.xpath(‘//div[@class="rich_media_content "]/p/strong/span/img/@data-src‘) obj_6 = obj.xpath(‘//div[@class="rich_media_content "]/p/img/@src‘) obj_body = obj_1 + obj_2 + obj_3 + obj_4 + obj_5 + obj_6 for i in obj_body: r = requests.get(i) with open(r"F:\day08\人民日报\微信图片\%s.jpg"%i[-30:-20],"wb") as f: f.write(r.content) body()