码迷,mamicode.com
首页 > 其他好文 > 详细

百度图片

时间:2017-04-22 14:37:35      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:gecko   ref   wow   路径   分析   pytho   print   tps   urllib   

import requests

from bs4 import BeautifulSoup

import urllib.request

import urllib.parse

import os

import re

import sys

j=0

keyword=input("请输入搜索信息:")#输入我们需要查询的信息

n=0#图片数

url=‘https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={word}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=word&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&cg=girl&pn=60&rn=rn&gsm=1e&1492587352169=‘

while(n<30*100):#当图片小于3000时,一直循环

    n=n+30#分析网页得,大约每网页有图片30,,所以每次加30。等于换页

    all_url=url.format(word={keyword},rn={str(n)})#获取搜寻图片的每页链接

    headers=\

        {

            "user-agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",

            "Referer":all_url

        }

    html=requests.get(all_url,headers=headers)#获取图片的某页全部信息

    content=html.text

    all_jpg=re.findall(r"thumbURL.+?jpg",content)#用正则表达式搜索我们需要的图片信息

    if os.path.isdir("E:\Python\Pythonprogram\百度美女图")!=True:#如果保存的路径不存在

        os.makedirs(r"E:\Python\Pythonprogram\百度美女图")#如果不存在。我们将创立这个路径

    with open("test.txt","w") as f#打开一个文本文件

        for jpg in all_jpg:#遍历所有的图片

            jpg=jpg.replace("thumbURL\":\"","")#将图片链接替换为空

            print(jpg)

            f.write(jpg)#将图片链接写入文件

            f.write(‘\n‘)

            try:

                with open("E:\Python\Pythonprogram\百度美女图\pic"+str(j)+".jpg","ab") as fp:#打开文件夹

                    print(requests.get(jpg,headers=headers).content)

                    fp.write(requests.get(jpg,headers=headers).content)#保存图片到路径

                #urllib.request.urlretrieve(jpg,"E:\Python\Pythonprogram\百度美女图\pic"+str(j)+".jpg")

                j+=1

            except Exception as e:

                pass

百度图片

标签:gecko   ref   wow   路径   分析   pytho   print   tps   urllib   

原文地址:http://www.cnblogs.com/Mdudu/p/6747561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!