抓取相簿¶
練習¶
In [ ]:
Copied!
import requests
from bs4 import BeautifulSoup
import requests
from bs4 import BeautifulSoup
In [ ]:
Copied!
url = "http://jimmyts.pixnet.net/album/set/2374457-pokemon-go"
url = "http://jimmyts.pixnet.net/album/set/2374457-pokemon-go"
In [ ]:
Copied!
# 抓取網站原始檔
r = requests.get(url)
# 抓取網站原始檔
r = requests.get(url)
In [ ]:
Copied!
# 使用BeautifulSoup做剖析網站原始檔
soup = BeautifulSoup(r.text)
# 使用BeautifulSoup做剖析網站原始檔
soup = BeautifulSoup(r.text)
In [ ]:
Copied!
# 使用 css selector 選取相簿內的圖片
image_list = soup.select('ul.photo-grid-list li a img')
# 使用 css selector 選取相簿內的圖片
image_list = soup.select('ul.photo-grid-list li a img')
In [ ]:
Copied!
# 檢查一下有沒有抓到圖片的 URL
for i, img in enumerate(image_list):
print(i, "==>", img.get('src'))
# 檢查一下有沒有抓到圖片的 URL
for i, img in enumerate(image_list):
print(i, "==>", img.get('src'))
In [ ]:
Copied!
# 將圖片存成圖片檔
for i, img in enumerate(image_list):
with open('{}.jpg'.format(i), 'wb') as f:
r = requests.get(img.get('src'))
f.write(r.content)
# 將圖片存成圖片檔
for i, img in enumerate(image_list):
with open('{}.jpg'.format(i), 'wb') as f:
r = requests.get(img.get('src'))
f.write(r.content)