在家无事做看三国杀皮肤画的不错,就随性爬取下来
# 爬取三国杀图片
from urllib.request import urlopen
from urllib.parse import urljoin
from re import findall,S
from os.path import basename
url = 'https://www.sanguosha.com/Home/newsInfo/a_id/20200109_7406_4617'
while True:
try:
with urlopen(url) as fp:
content = fp.read().decode('utf-8')
result = findall(r'新品皮肤一览.*?下一篇.*?/div>', content, S)
resultpic = findall(r'<img src="(.*?)"', str(result))
if resultpic!=[]:
for picurl in resultpic:
picurl = urljoin(url, picurl)
with urlopen(picurl)as fd:
with open('C://Users//Administrator//Pictures'+'//'+basename(picurl), 'wb') as fp:
fp.write(fd.read())
url = str(findall(r'下一篇.*?href="(.*?)"',content, S)[0])
print(url)
except:
break
但是有个问题三国杀最后一页仍有下一篇,不会自己停止,运行一会手动停了吧!