-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigrate_spider.py
More file actions
70 lines (53 loc) · 1.61 KB
/
Copy pathmigrate_spider.py
File metadata and controls
70 lines (53 loc) · 1.61 KB
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
# coding:utf-8
import requests
import pymongo
import datetime
import time
base_url = 'https://report.amap.com/cityTravel/inAndOutCity.do'
myclient = pymongo.MongoClient("mongodb://localhost:27017")
mydb = myclient["move"]
coll = mydb["migrateIn"]
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36 Edg/86.0.622.38',
'cookie': 'user_unique_id=a187b9ae74ba457e017524cc42e37fbc'
}
def get_response(date):
params = {
'adcode': '210200',
'dt': date,
'willReal': 'WILL',
'size': '100',
'inOut': 'IN'
}
try:
response = requests.get(base_url, params=params, headers=headers)
result = response.text
except Exception as why:
print(why)
print('url:', response.url)
print('text:', response.text)
result = ''
return result
def getdaylist():
beginDate = datetime.datetime.strptime("2020-01-02", "%Y-%m-%d")
endDate = datetime.datetime.strptime("2020-10-12", "%Y-%m-%d")
dayList = []
while beginDate <= endDate:
dayList.append(datetime.datetime.strftime(beginDate, "%Y-%m-%d"))
beginDate += datetime.timedelta(days=+1)
return dayList
def insert_db():
dayList = getdaylist()
for day in dayList:
print(day)
res = get_response(day)
if res:
try:
coll.insert_one({
"date": day,
"data": res
})
except Exception as why:
print(why)
time.sleep(1.5)
insert_db()