Last active
September 25, 2018 13:14
-
-
Save RuolinZheng08/00488723a5fec0358087a4a83af5c596 to your computer and use it in GitHub Desktop.
[Python] Scrap thumbnails from WeChat Official Account Posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from urllib.request import urlopen | |
import ssl | |
import re, os, sys | |
# ignore ssl certificate errors | |
ctx = ssl.create_default_context() | |
ctx.check_hostname = False | |
ctx.verify_mode = ssl.CERT_NONE | |
# go to desktop | |
try: | |
desktop = os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') | |
os.chdir(desktop) | |
except Exception as e: | |
print(f'<Error>{e}\n') | |
sys.exit(1) | |
while True: | |
try: | |
print('<Press the space bar to exit>') | |
link = input('Enter link to the post:') | |
fname = input('Save the image as:') | |
if not len(link) or not len(fname): | |
break | |
src = urlopen(link, context=ctx).read().decode('utf-8') | |
url = re.findall(r'var msg_cdn_url = "(\S+)";', src)[0] | |
fmt = url.split('fmt=')[1] | |
fname = f'{fname}.{fmt}' | |
print(f'Downloading the thumbnail from {url}...') | |
with open(fname, 'wb') as fout: | |
data = urlopen(url).read() | |
if not data: | |
print('<Error> Failed to download the image') | |
break | |
fout.write(data) | |
print(f'The image <{fname}> has been saved to {(os.getcwd())}\n') | |
except Exception as e: | |
print('<Error>', e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment