-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcraigslist_api.py
184 lines (167 loc) · 9.57 KB
/
craigslist_api.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import json
import re
import time
import requests
from bs4 import BeautifulSoup
from email_api import email
from urllib.parse import urlparse
class craigslist():
session = None
email_ad = ''
def __init__(self):
global session
session = requests.session()
def save_cookies(self): # 保存cookie
global session, path_for
with open('./' + "craigslistcookiefile", 'w')as f:
json.dump(session.cookies.get_dict(), f)
def read_cookies(self): # 使用cookie
global session, path_for
# _session.cookies.load()
# _session.headers.update(header_data)
with open('./' + 'craigslistcookiefile')as f:
cookie = json.load(f)
session.cookies.update(cookie)
def sign_up_and_login(self):
e = email()
email_address = e.get_emailaddress()
self.email_ad = email_address
r = session.post('https://accounts.craigslist.org/signup',
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://accounts.craigslist.org',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://accounts.craigslist.org/login'}, data={
'emailAddress': email_address,
'rq': '/login/home',
'rt': 'L',
't': int(time.time())
})
if 'Thanks for signing up for a craigslist account!' in r.text:
print('Thanks for signing up for a craigslist account!')
print("_--------------------------------------------------------")
else:
print('sign up error')
login_url = ''
while True:
print('e.get_content_by_craigslist()')
login_url = e.get_content_by_craigslist()
if not login_url:
time.sleep(10)
else:
print(login_url)
break
o = urlparse(login_url)
query_data = {x.split('=')[0]: x.split('=')[1] for x in o.query.split('&')}
query_data['goPasswordless'] = '1'
r = session.post('https://accounts.craigslist.org/pass',
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://accounts.craigslist.org',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': login_url}, data=query_data)
print(r.status_code)
print('_________________---=--------------------')
print('cookies')
print(session.cookies)
self.save_cookies()
def account(self):
self.read_cookies()
r = session.get('https://accounts.craigslist.org/login/home')
print('*********************************home')
print(r.status_code)
def accept(self):
self.read_cookies()
query_data = {
'step': 'touAccepted',
'rt': '',
'rp': ''
}
r = session.post('https://accounts.craigslist.org/login/tou',
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://accounts.craigslist.org',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://accounts.craigslist.org/login/tou'}, data=query_data)
print('*********************************accept')
print(r.status_code)
def create_post(self):
self.read_cookies()
r = session.get('https://post.craigslist.org/c/tor',
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://post.craigslist.org',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'})
print('*********************************create_post')
print(r.text)
print(r.status_code)
soup = BeautifulSoup(r.text, 'html.parser')
tags = soup.findAll('form', action=re.compile("https://post.craigslist.org/"))
post_url = tags[0]['action']
cryptedStepCheck = soup.find_all('input', {"name": "cryptedStepCheck"})[0]['value']
subarea_data = {}
session.get(post_url + '?s=subarea', headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'},
)
r = session.post(post_url,
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'},
data={'n': '1', 'cryptedStepCheck': cryptedStepCheck})
print('1______________________________________')
print(post_url)
print(r.status_code)
session.get(post_url + '?s=type', headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'})
r = session.post(post_url,
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'},
data={'id': 'ho', 'cryptedStepCheck': cryptedStepCheck})
print('2________________________________________')
print(r.status_code)
r = session.post(post_url,
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'},
data={'id2': '2019x810X2019x576X2048x1152', 'id': '18', 'cryptedStepCheck': cryptedStepCheck})
print('3_________________________________________')
print(r.status_code)
data = {"id2": "1411x1245X1411x568X1440x900",
"PostingTitle": "Toronto+student+homestay",
"GeographicArea": "Toronto",
"postal": "M2J0B3",
"PostingBody": "Please+check+out+this+homestay%0D%0Ahttps%3A%2F%2Fwww.homadorma.com%2Fen%2Fhomestay%2FHS104267",
"price": "",
"Sqft": "0",
"private_room": "0",
"private_bath": "0",
"housing_type": "6",
"laundry": "",
"parking": "",
"movein_date": "",
"FromEMail": self.email_ad,
"Privacy": "C",
"go": "continue",
"cryptedStepCheck": "U2FsdGVkX18zMTM5ODMxMx-_4o5EoXILYZOz0Ebyl5G7RWC-3j0KCQDBw8F0dVvK",
}
r = session.post(post_url,
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'},
data=data)
print('4_________________________________________')
print(r.status_code)
r = session.get('https://accounts.craigslist.org/login/home?show_tab=drafts',
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Origin': 'https://toronto.craigslist.org/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Referer': 'https://toronto.craigslist.org/'})
print('5_________________________________________')
print(r.status_code)
print(r.text)