使用爬虫爬取微博账户图片
程序员文章站
2024-03-08 10:46:16
...
使用Python3.7爬取微博照片
import re
import os,stat
import urllib.request
file_path='F:/python/picture/img6/'
cnt=0
ispath=os.path.exists(file_path)
if not ispath:
os.mkdir(file_path)
with open('2.txt') as fr:
data=fr.read()
temp = data.split('"')
strlist=[]
for i in temp:
# print(i)
res = re.findall('(.*?)jpg',i)
if res:
if 'gma.alicdn.com' not in res:
sttemp=str(res).replace('[','').replace(']','').replace('\'','').replace('\\','')+'jpg'
if 'orj360' in sttemp:
cnt= cnt +1
print(cnt)
rstr=sttemp.replace('orj360','large')
print(rstr)
if cnt>58:
rs=urllib.request.urlopen(rstr)
with open(file_path+str(cnt)+'.jpg','wb') as fw:
fw.write(rs.read())
fw.close()
print(cnt)
print('下载完成')
使用了两个库函数,爬取速度也挺快的!!