记第一次爬虫

出不来结果的时候,真是着急,期间犯了很多错误,这个过程痛苦并快乐着

哈哈哈哈哈,哈哈哈哈哈

import urllib.request
import re
import os
url='https://www.52pojie.cn/home.php?mod=medal'
page=urllib.request.urlopen(url).read()

page=page.decode('gbk')
z_page=r'<ul class="mtm mgc1 c1">(.+?)</u1>'
zz=r'<img src="https://static.52pojie.cn/static/image/common/(.+?)" alt="(.+?)" style=".+?">'
html=re.findall(zz,page,re.S)
print(html)

def hh(filepath):
    s=os.path.exists(filepath)
    if not s:
        os.makedirs(filepath)
        print("创建文件")
    else:
        print("文件已存在!")
r="C:/Users/Administrator/Desktop/吾爱破解图片/"
hh(r)
i=int(0)
for line in html:
    line=html[i]
    if str(line[0]).endswith('gif'):
        m1=line[0]
        m2=line[1]
        print(m2+" "+m1)
        url="https://static.52pojie.cn/static/image/common/"+m1
        web=urllib.request.urlopen(url)
        data=web.read()
        with open("r",'wb') as f:
            f=open(r+m2+'.gif','wb')
            f.write(data)
        f.close()
    i+=1



,在最后卡到能下载图片,但不是指定文件夹,后来才发现,我天,我少加了一个反斜杠,泪奔。



总之,还是成功了,我的编程再也不是做算术题了































猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/81050485