实战利用py爬取图片

记第一次写爬虫

作为只回看懂代码的我,今玩要自己动手写一次爬虫,以前看过BeautifulSouprequests库的一些用法,现在要自己写一个,花了我一个晚上的时间,结果写完也只有十几行的代码,难受受,看着自己写的十几行辣鸡代码,看来自己以后得好好自己动手写代码了.

实战爬取图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#coding=utf-8
import requests
from bs4 import BeautifulSoup
import urllib
url = 'https://mp.weixin.qq.com/s/_ejalkc-Dg-YsFEXfVNpVw'

response = requests.get(url)

html = response.text

soup = BeautifulSoup(html,'html.parser')

dd = soup.find_all('img',attrs={'data-src':True}) # 找到属性值(即图片地址)
b=[] #建一个列表放图片地址

for img in dd:
b.append(img.attrs['data-src']) #将获取的地址放到列表中,我也不知道自己为什么脑残这么做
x=1
for imgurl in b:
urllib.urlretrieve(imgurl,'%s.jpg'%x) #感觉用requests来编写下载的代码好麻烦就用urllib了 (逃~
x = x + 1

菜鸡又得自闭了 ==