求问大神,怎么用Python编码,完成图片中的内容~

求问大神,怎么用Python编码,完成图片中的内容~,第1张

from __future__ import print_function

import sys

if sysversion[0]=='2':input=raw_input

def isprime(num):

    if num<2:return False

    for i in range(2,num):

        if num%i==0:break

    else:

        return True

    return False

n=int(input())

arr=[int(x) for x in input()split()]

for i in range(n):

    if isprime(arr[i]):

        print("YES",end=' ')

    else:

        print("NO",end=' ')

print()

1)首先你要明白爬虫怎样工作。

想象你是一只蜘蛛,现在你被放到了互联“网”上。那么,你需要把所有的网页都看一遍。怎么办呢?没问题呀,你就随便从某个地方开始,比如说人民日报的首页,这个叫initial pages,用$表示吧。

在人民日报的首页,你看到那个页面引向的各种链接。于是你很开心地从爬到了“国内新闻”那个页面。太好了,这样你就已经爬完了俩页面(首页和国内新闻)!暂且不用管爬下来的页面怎么处理的,你就想象你把这个页面完完整整抄成了个html放到了你身上。

突然你发现, 在国内新闻这个页面上,有一个链接链回“首页”。作为一只聪明的蜘蛛,你肯定知道你不用爬回去的吧,因为你已经看过了啊。所以,你需要用你的脑子,存下你已经看过的页面地址。这样,每次看到一个可能需要爬的新链接,你就先查查你脑子里是不是已经去过这个页面地址。如果去过,那就别去了。

好的,理论上如果所有的页面可以从initial page达到的话,那么可以证明你一定可以爬完所有的网页。

那么在python里怎么实现呢?

很简单

import Queue

initial_page = "初始化页"

url_queue = QueueQueue()

seen = set()

seeninsert(initial_page)

url_queueput(initial_page)

while(True): #一直进行直到海枯石烂

if url_queuesize()>0:

current_url = url_queueget() #拿出队例中第一个的url

store(current_url) #把这个url代表的网页存储好

for next_url in extract_urls(current_url): #提取把这个url里链向的url

if next_url not in seen:

seenput(next_url)

url_queueput(next_url)

else:

break

写得已经很伪代码了。

所有的爬虫的backbone都在这里,下面分析一下为什么爬虫事实上是个非常复杂的东西——搜索引擎公司通常有一整个团队来维护和开发。

2)效率

如果你直接加工一下上面的代码直接运行的话,你需要一整年才能爬下整个豆瓣的内容。更别说Google这样的搜索引擎需要爬下全网的内容了。

问题出在哪呢?需要爬的网页实在太多太多了,而上面的代码太慢太慢了。设想全网有N个网站,那么分析一下判重的复杂度就是Nlog(N),因为所有网页要遍历一次,而每次判重用set的话需要log(N)的复杂度。OK,OK,我知道python的set实现是hash——不过这样还是太慢了,至少内存使用效率不高。

通常的判重做法是怎样呢?Bloom Filter 简单讲它仍然是一种hash的方法,但是它的特点是,它可以使用固定的内存(不随url的数量而增长)以O(1)的效率判定url是否已经在set中。可惜天下没有白吃的午餐,它的唯一问题在于,如果这个url不在set中,BF可以100%确定这个url没有看过。但是如果这个url在set中,它会告诉你:这个url应该已经出现过,不过我有2%的不确定性。注意这里的不确定性在你分配的内存足够大的时候,可以变得很小很少。一个简单的教程:Bloom Filters by Example

注意到这个特点,url如果被看过,那么可能以小概率重复看一看(没关系,多看看不会累死)。但是如果没被看过,一定会被看一下(这个很重要,不然我们就要漏掉一些网页了!)。 [IMPORTANT: 此段有问题,请暂时略过]

好,现在已经接近处理判重最快的方法了。另外一个瓶颈——你只有一台机器。不管你的带宽有多大,只要你的机器下载网页的速度是瓶颈的话,那么你只有加快这个速度。用一台机子不够的话——用很多台吧!当然,我们假设每台机子都已经进了最大的效率——使用多线程(python的话,多进程吧)。

3)集群化抓取

爬取豆瓣的时候,我总共用了100多台机器昼夜不停地运行了一个月。想象如果只用一台机子你就得运行100个月了

那么,假设你现在有100台机器可以用,怎么用python实现一个分布式的爬取算法呢?

我们把这100台中的99台运算能力较小的机器叫作slave,另外一台较大的机器叫作master,那么回顾上面代码中的url_queue,如果我们能把这个queue放到这台master机器上,所有的slave都可以通过网络跟master联通,每当一个slave完成下载一个网页,就向master请求一个新的网页来抓取。而每次slave新抓到一个网页,就把这个网页上所有的链接送到master的queue里去。同样,bloom filter也放到master上,但是现在master只发送确定没有被访问过的url给slave。Bloom Filter放到master的内存里,而被访问过的url放到运行在master上的Redis里,这样保证所有操作都是O(1)。(至少平摊是O(1),Redis的访问效率见:LINSERT – Redis)

考虑如何用python实现:

在各台slave上装好scrapy,那么各台机子就变成了一台有抓取能力的slave,在master上装好Redis和rq用作分布式队列。

代码于是写成

#slavepy

current_url = request_from_master()

to_send = []

for next_url in extract_urls(current_url):

to_sendappend(next_url)

store(current_url);

send_to_master(to_send)

#masterpy

distributed_queue = DistributedQueue()

bf = BloomFilter()

initial_pages = "wwwrenmingribaocom"

while(True):

if request == 'GET':

if distributed_queuesize()>0:

send(distributed_queueget())

else:

break

elif request == 'POST':

bfput(requesturl)

好的,其实你能想到,有人已经给你写好了你需要的:darkrho/scrapy-redis · GitHub

4)展望及后处理

虽然上面用很多“简单”,但是真正要实现一个商业规模可用的爬虫并不是一件容易的事。上面的代码用来爬一个整体的网站几乎没有太大的问题。

但是如果附加上你需要这些后续处理,比如

有效地存储(数据库应该怎样安排)

有效地判重(这里指网页判重,咱可不想把人民日报和抄袭它的大民日报都爬一遍)

有效地信息抽取(比如怎么样抽取出网页上所有的地址抽取出来,“朝阳区奋进路中华道”),搜索引擎通常不需要存储所有的信息,比如我存来干嘛

及时更新(预测这个网页多久会更新一次)

如你所想,这里每一个点都可以供很多研究者十数年的研究。虽然如此,

“路漫漫其修远兮,吾将上下而求索”。

所以,不要问怎么入门,直接上路就好了:)

你的 print 语句没有在 for 循环里,你需要在输入下面三个 print 语句的时候,缩进到 for 里面。

看得出来你是在命令行里直接运行 python 解释器的,当你输入完

for magician in magicians:

以后,解释器进入循环,等待你输入循环中的语句。

所以,在这之后的语句都要从 for 开始缩进,每次先按四个空格缩进,输入pirnt语句后回车,三个 print 输入完后,直接按回车,就可以运行循环了。

按四下空格,输入第一个 print 语句,回车

按四下空格,输入第一个 print 语句,回车

按四下空格,输入第三个 print 语句,回车

这时候三个语句输入完了,直接按下回车,程序就会运行循环里的三个语句。

循环执行完以后就退出了,接下来的语句就不在循环里了,就要从行首继续输入了。

import media

def red_average(pic):

'''Return an integer that represents the average red of the picture

'''

total=0

for pixel in pic:

total = total + mediaget_red(pixel)

red_average = total / (mediaget_width(pic)mediaget_height(pic))

return red_average

def green_average(pic):

'''Return an integer that represents the average green of the picture

'''

total = 0

for pixel in pic:

total = total + mediaget_green(pixel)

green_average = total / (mediaget_width(pic)mediaget_height(pic))

return green_average

def blue_average(pic):

'''Return an integer that represents the average blue of the picture

'''

total = 0

for pixel in pic:

total = total + mediaget_blue(pixel)

blue_average = total / (mediaget_width(pic)mediaget_height(pic))

return blue_average

def scale_red(pic, value):

'''Return the picture that the average of the red is value which has been set

'''

averaged = red_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_red = min(255, int(factor mediaget_red(pixel)))

mediaset_red(pixel,new_red)

return pic

def scale_green(pic, value):

'''Return the picture that the average of the green is value which has been set

'''

averaged = green_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_green = min(255, int(factor mediaget_green(pixel)))

mediaset_green(pixel,new_green)

return pic

def scale_blue(pic, value):

'''Return the picture that the average of the blue is value which has been set

'''

averaged = blue_average(pic)

factor = float(value) / averaged

for pixel in pic:

new_blue = min(255, int(factor mediaget_blue(pixel)))

mediaset_blue(pixel,new_blue)

return pic

def expand_height(pic, factor):

'''Return a newpicture that has been vertically stretched by the factor which has been set

'''

new_width = picget_width()

new_height = picget_height()factor

newpic = mediacreate_pic(new_width, new_height, mediablack)

for pixel in pic:

x = mediaget_x(pixel)

y = mediaget_y(pixel)

newpixel = mediaget_pixel(newpic, x, yfactor)

for newpixel in newpic:

new_red = mediaget_red(pixel)

new_green = mediaget_green(pixel)

new_blue = mediaget_blue(pixel)

mediaset_red(newpixel,new_red)

mediaset_green(newpixel,new_green)

mediaset_blue(newpixel,new_blue)

return newpic

def expand_width(pic,factor):

'''Return a newpicture that has been horizontally stretched by the factor which has been set

'''

new_width = picget_width() factor

new_height = picget_height()

newpic = mediacreate_pic(new_width,new_height,mediablack)

for newpixel in newpic:

x = mediaget_x(newpixel)

y = mediaget_y(newpixel)

pixel = mediaget_pixel(pic,x / factor, y)

new_red = mediaget_red(pixel)

new_green = mediaget_green(pixel)

new_blue = mediaget_blue(pixel)

mediaset_red(newpixel,new_red)

mediaset_green(newpixel,new_green)

mediaset_blue(newpixel,new_blue)

return newpic

def reduce_height(pic, factor):

'''return a new pic that has been compressed vertically by the factor which has been set

'''

# Create a new, all-black pic with the appropriate new height and

# old width; (all colour components are zero)

new_width = picget_width

new_height = (picget_height() - 1) / factor + 1

newpic = mediacreate_pic(new_width, new_height, mediablack)

# Iterate through all the pixels in the original (large) image, and copy

# a portion of each pixel's colour components into the correct

# pixel position in the smaller image

for pixel in pic:

# Find the corresponding pixel in the new pic

x = mediaget_x(pixel)

y = mediaget_y(pixel)

newpixel = mediaget_pixel(newpic, x, y / factor)

# Add the appropriate fraction of this pixel's colour components

# to the components of the corresponding pixel in the new pic

new_red = newpixelget_red()+pixelget_red()/factor

new_green = newpixelget_green()+pixelget_green()/factor

new_blue = newpixelget_blue()+pixelget_blue()/fctor

mediaset_red(newpixel,new_red)

mediaset_green(newpixel,new_green)

mediaset_blue(newpixel,new_blue)

return newpic

def reduce_width(pic,factor):

'''Return a newpic that has been horizontally compressed by the factor which has been set

'''

new_width = (mediaget_width() - 1) / factor + 1

new_height = mediaget_height()

newpic = mediacreate_pic(new_width, new_height, mediablack)

for pixel in pic:

x = mediaget_x(pixel)

y = mediaget_y(pixel)

new_pixel = mediaget_pixel(newpic, x / factor, y)

new_red = newpixelget_red() + pixelget_red() / factor

new_green = newpixelget_green() + pixelget() / factor

new_blue = newpixelget_blue() + pixelget()/factor

mediaset_red(newpixel, new_red)

mediaset_green(newpixel, new_green)

mediaset_blue(newpixel, new_blue)

return newpic

def distance(pixel1, pixel2):

red1 = mediaget_red(pixel1)

green1 = mediaget_green(pixel1)

blue1 = mediaget_blue(pixel1)

red2 = mediaget_red(pixel2)

green2 = mediaget_green(pixel2)

blue2 = mediaget_blue(pixel2)

sum = abs(red1 -red2) + abs(green1 - green2) + abs(blue1 - blu2)

return sum

def simple_difference(pic1, pic2):

for pixel in pic1:

x = mediaget_x(pixel)

y = mediaget_y(pixel)

pixel2 = mediaget_pixel(pic2, x, y)

sum = mediadistance(pixel, pixel2)

return sum

def smart_difference(pic1,pic2):

height1 = mediaget_height(pic1)

height2 = mediaget_height(pic2)

factorh = float(height1 / height2)

if factorh >= 1:

height1 = mediareduce_height(pic1, factorh)

else:

height2 = mediareduce_height(pic2, 1 / factorh)

width1 = mediaget_width(pic1)

width2 = mediaget_width(pic2)

factorw = float(width1 / width2)

if factorw >= 1:

width1 = reduce_width(pic1, factorw)

else:

width2 = reduce_width(pic2, 1 / factorw)

red1 = red_average(pic1)

green1 = green_average(pic1)

blue1 = blue_average(pic1)

red2 = mediascale_red(pic2, red1)

green2 = mediascale_green(pic2, green1)

blue2 = mediascale_blue(pic2, blue1)

#if __name__ == '__main__':

#mediashow(newpic)

  在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作。本人偏爱 matpoltlib,因为它的语法更像 matlab。

  一、matplotlib

  1 显示

  复制代码

  import matplotlibpyplot as plt # plt 用于显示

  import matplotlibimage as mpimg # mpimg 用于读取

  import numpy as np

  lena = mpimgimread('lenapng') # 读取和代码处于同一目录下的 lenapng

  # 此时 lena 就已经是一个 nparray 了,可以对它进行任意处理

  lenashape #(512, 512, 3)

  pltimshow(lena) # 显示

  pltaxis('off') # 不显示坐标轴

  pltshow()

  复制代码

  2 显示某个通道

  复制代码

  # 显示的第一个通道

  lena_1 = lena[:,:,0]

  pltimshow('lena_1')

  pltshow()

  # 此时会发现显示的是热量图,不是我们预想的灰度图,可以添加 cmap 参数,有如下几种添加方法:

  pltimshow('lena_1', cmap='Greys_r')

  pltshow()

  img = pltimshow('lena_1')

  imgset_cmap('gray') # 'hot' 是热量图

  pltshow()

  复制代码

  3 将 RGB 转为灰度图

  matplotlib 中没有合适的函数可以将 RGB 图转换为灰度图,可以根据公式自定义一个:

  复制代码

  def rgb2gray(rgb):

  return npdot(rgb[,:3], [0299, 0587, 0114])

  gray = rgb2gray(lena)

  # 也可以用 pltimshow(gray, cmap = pltget_cmap('gray'))

  pltimshow(gray, cmap='Greys_r')

  pltaxis('off')

  pltshow()

  复制代码

  4 对图像进行放缩

  这里要用到 scipy

  复制代码

  from scipy import misc

  lena_new_sz = miscimresize(lena, 05) # 第二个参数如果是整数,则为百分比,如果是tuple,则为输出图像的尺寸

  pltimshow(lena_new_sz)

  pltaxis('off')

  pltshow()

  复制代码

  5 保存图像

  51 保存 matplotlib 画出的图像

  该方法适用于保存任何 matplotlib 画出的图像,相当于一个 screencapture。

  pltimshow(lena_new_sz)

  pltaxis('off')

  pltsavefig('lena_new_szpng')

  52 将 array 保存为图像

  from scipy import misc

  miscimsave('lena_new_szpng', lena_new_sz)

  53 直接保存 array

  读取之后还是可以按照前面显示数组的方法对图像进行显示,这种方法完全不会对图像质量造成损失

  npsave('lena_new_sz', lena_new_sz) # 会在保存的名字后面自动加上npy

  img = npload('lena_new_sznpy') # 读取前面保存的数组

  二、PIL

  1 显示

  from PIL import Image

  im = Imageopen('lenapng')

  imshow()

  2 将 PIL Image 转换为 numpy 数组

  im_array = nparray(im)

  # 也可以用 npasarray(im) 区别是 nparray() 是深拷贝,npasarray() 是浅拷贝

  3 保存 PIL

  直接调用 Image 类的 save 方法

  from PIL import Image

  I = Imageopen('lenapng')

  Isave('new_lenapng')

  4 将 numpy 数组转换为 PIL

  这里采用 matplotlibimage 读入数组,注意这里读入的数组是 float32 型的,范围是 0-1,而 PILImage 数据是 uinit8 型的,范围是0-255,所以要进行转换:

  import matplotlibimage as mpimg

  from PIL import Image

  lena = mpimgimread('lenapng') # 这里读入的数据是 float32 型的,范围是0-1

  im = Imagefromarray(npuinit8(lena255))

  imshow()

  5 RGB 转换为灰度图

  from PIL import Image

  I = Imageopen('lenapng')

  Ishow()

  L = Iconvert('L')

  Lshow()

无法展示或动画,也无法为您提供实时执行代码的服务。以下是Python 3中的色蟒蛇代码示例,您可以尝试将其复制到您的Python IDE中运行:```pythonimport turtle# Set up color listcolors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']# Set up turtlesnake = turtleTurtle()snakespeed(10)snakepenup()snakegoto(-150, 0)snakependown()# Draw snakefor i in range(360): snakepencolor(colors[i%6]) snakewidth(i/100 + 1) snakeforward(i) snakeleft(59) turtledone()```

欢迎分享,转载请注明来源:表白网

原文地址:https://h5.hunlipic.com/biaobai/4042253.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2024-04-12
下一篇2024-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存