import requests from lxml import etree headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36', } url = 'http://sc.chinaz.com/tupian/' response = requests.get(url=url,headers=headers) response.encoding = 'utf-8' page_text = response.text #解析页面数据(获取页面中的图片链接) #创建etree对象 tree = etree.HTML(page_text) div_list = tree.xpath('//div[@id="container"]/div') #解析获取图片地址和图片的名称 #('.//img/@src2') 伪属性 for div in div_list: image_url = div.xpath('.//img/@src2') image_name = div.xpath('.//img/@alt') print(image_name) print(image_url)