How to download Wechat avatar with one click of Python
This article mainly explains "how to use Python button to download Wechat avatar", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "how to use Python button to download Wechat avatar" bar!
1 requests failed
Use requests to request Wechat emoji URL https://www.zhihu.com/question/335825565, and use web web page extraction package: lxml to extract all the desired image link addresses, that is, the following code:
Import requests
From lxml import etree
Import pandas as pd
Import re
Url = 'https://www.zhihu.com/question/335825565'
With requests.get (url) as res:
Content = res.content
Print (content)
Html = etree.HTML (content)
Imgs = html.xpath ('/ / figure//img/@src')
Print (imgs)
Run directly like this, and the content print is shown below, and no desired results are requested.
B'\ r\ n400 Bad Request\ r\ n\ r\ n400 Bad Request\ r\ nopenresty\ r\ n\ r\ n\ r\ ndirectly extract
To take a direct approach, open the web page https://www.zhihu.com/question/335825565 https://www.zhihu.com/question/335825565 F12 to view the structure of the web page:
Directly copy all the contents of this tag to the local, and extract the address of all the pictures.
Select the above tag, right-> copy- > copy element and directly copy all the contents of this tag locally and save it as wechat_pic.html
Next, extract the addresses of all the pictures and extract them directly using python's regular module.
Step 1: import the package used
Import os
Import re
From urllib.request import urlopen
The second step to the last step: read in the tag copied above, use the regular expression to extract the required image url, then use urlopen to download the image file from this url, and save it locally. There are some details here. Readers are advised to try running after downloading the code.
With open ('. / data/wechat_pic.html',mode='r',errors='ignore') as f:
Lines = f.readlines ()
Pic_urls = re.findall (r')