import urllib.request
from lxml import etree
import os
def handle_url(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537'
}
request = urllib.request.Request(url=url, headers=headers)
return request
def download_image(image_url_list, name_list):
# print(image_url_list)
# print(len(name_list), len(image_url_list))
# exit()
# 获取文件夹名字
dirpath = './images'
# 遍历url列表,和名字列表
for i in range(len(name_list)):
# 根据url获取到图片的后缀名
suffix = os.path.splitext(image_url_list[i])[-1]
# 得到图片的全路径
filepath = os.path.join(dirpath, name_list[i]) +suffix
# 下载图片
try:
urllib.request.urlretrieve(image_url_list[i], filepath)
print('%s 下载完毕' % filepath)
except Exception as e:
print('%s xxxxxxxxxxxxx图片丢失' % filepath)
def handle_data(request):
response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')
html_tree = etree.HTML(html)
# print(type(html_tree))
# //div[@id="container"]/div/div/a/img/@src
image_url_list = html_tree.xpath('//div[@id="container"]/div/div/a/img/@src2')
# print(len(image_url_list))
# exit()
name_list = html_tree.xpath('//div[@id="container"]/div/div/a/img/@alt')
# 遍历image_url_list,依次下载图片
download_image(image_url_list, name_list)
def main():
start_page = int(input('请输入起始页面:'))
end_page = int(input('请输入结束页面:'))
url_tmp = 'http://sc.chinaz.com/tupian/xingganmeinvtupian'
print('开始下载图片')
for page in range(start_page, end_page + 1):
if page != 1:
url = url_tmp + '_' + str(page) + '.html'
else:
url = url_tmp + '.html'
request = handle_url(url)
handle_data(request)
print('结束下载图片')
if __name__ == '__main__':
main()
用xpath解析网页下载美女图片
最新推荐文章于 2024-07-31 00:53:52 发布