爬去图片演讲

Posted futureholmes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬去图片演讲相关的知识,希望对你有一定的参考价值。

import requests
from lxml import html

# http://pic.netbian.com/uploads/allimg/180826/113958-1535254798fc1c.jpg
# http://pic.netbian.com/4kfengjing/index_3.html
# all_url = "http://pic.netbian.com/4kfengjing/index_{}.html".format(1)


url = "http://pic.netbian.com/4kfengjing/"
response = requests.request(‘get‘, url=url)
html_etree = html.etree.HTML(response.content.decode(‘gbk‘))  # type = <class   ‘lxml.._etreeElement‘>

img_url = html_etree.xpath("//ul[@class=‘clearfix‘]/li/a/img/@src")
b_content = html_etree.xpath("//ul[@class=‘clearfix‘]/li/a/b/text()")


def get_response(url):
    response = requests.request(‘get‘, url=url)
    return response.content


for i in range(20):
    img_url_true = "http://pic.netbian.com/" + img_url[i]
    print(img_url_true)
    content = get_response(img_url_true)
    with open(‘img/{}.jpg‘.format(b_content[i]), ‘wb+‘) as f:
        f.write(content)

以上是关于爬去图片演讲的主要内容,如果未能解决你的问题,请参考以下文章

Python爬去知乎上问题下所有图片

Python(16)_爬去百度图片(urlopen和urlretrieve)

Python 爬去糗事百科内容讲解

根据图片的url地址下载图片到本地保存代码片段

制作简单文本爬虫

如何用几行代码读取目录下所有的图片