首先,我们需要找到可爬取的图片网址
https://www.qqtn.com/tx/katongtx_1.html
F12,找到network后再次刷新页面
hearders处可看到请求
在response可看到该页面的整个源码
首先我们得导入requests包,(若没有可以使用命令 pip install requests 自行下载)
定义一个url 并向该网页发送请求(可能会出现乱码,使用encoding解码,该网页显然用utf-8并不能解码,不过形式与网页源码相似)
import requests
url='https://www.qqtn.com/tx/katongtx_1.html'
response=requests.get(url=url)
response.encoding='utf-8'
print(response.text)
在该网页的源码中取图片,用正则表达式解析
urls=re.findall('<img src="(.*?)" alt=".*?" />',html)
得到图片链接集合列表
for循环便利每个图片,以二进制写入本地文件中
完整代码
import requests,re,time
url='https://www.qqtn.com/tx/katongtx_1.html'
response=requests.get(url=url)
response.encoding='utf-8'
html=response.text
#解析
urls=re.findall('<img src="(.*?)" alt=".*?" />',html)
for url in urls:
time.sleep(1)
response = requests.get(url=url[0])
with open(url, 'wb') as k:
k.write(response.content)