-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.Scraping_urls.py
89 lines (49 loc) · 1.49 KB
/
02.Scraping_urls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import time
import json
import requests
import numpy as np
import pandas as pd
from PIL import Image
from io import BytesIO
# In[2]:
time_type = {
'a1': '10点前',
'n1': '10~12点',
'n2': '12~14点',
'p1': '14点后'
}
# In[3]:
SkiRanch_ID = '57a0ff6a-8619-4a78-976b-58a0b971ba66' # 崇礼 万龙度假天堂
DailyPhotoAPI = 'https://api.fenxuekeji.com/api/pw/photo_walls/%s/daily'
date_string = '2019-03-31'
apn = 'n1'
# In[4]:
params = {'apn': apn, 'datestring': date_string, 'device': 'pc', 'page': 1}
meta_data = requests.get(DailyPhotoAPI %SkiRanch_ID, params).json()['data']
total_pages = meta_data['total_pages']
# In[5]:
# Init.
urls = pd.Series()
# Save 1st page
urls = urls.append(pd.DataFrame(dict(pd.DataFrame(meta_data['photos'])['image'])).loc['x1000'])
# Retrieve pages
for page_idx in np.arange(2,total_pages):
# Set parameters
params = {'apn': apn, 'datestring': date_string, 'device': 'pc', 'page': page_idx}
# Get metadata
meta_data = requests.get(DailyPhotoAPI %SkiRanch_ID, params).json()['data']
# Save urls
urls = urls.append(pd.DataFrame(dict(pd.DataFrame(meta_data['photos'])['image'])).loc['x1000'])
# Pause to avoid getting banned
time.sleep(np.random.choice(np.arange(1,10)))
# Save urls
urls = urls.reset_index()[0]
urls.to_csv('pw_20190331_wl_n1.csv')
# In[6]:
image = Image.open(BytesIO(requests.get(np.random.choice(urls)).content))
# In[7]:
image
# In[ ]: