-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawler-web.py
53 lines (36 loc) · 1.42 KB
/
crawler-web.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
# -*- coding: utf-8 -*-
import web
import sys
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
urls = (
'/officialaccount', 'officialAccount'
)
class officialAccount:
def GET(self):
data = []
i = web.input()
web.header('Content-Type', 'text/json; charset=utf-8', unique=True)
driver = webdriver.PhantomJS(executable_path=[YOUR-PhantomJS-PATH])
# For example, In my Mac: driver = webdriver.PhantomJS(executable_path='/Users/ashun/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs')
driver.get('https://www.newrank.cn/public/info/detail.html?account=' + i.id)
wait = WebDriverWait(driver, 20)
wait.until(EC.presence_of_element_located((By.ID, 'info_detail_article_top')))
for title in driver.find_elements_by_css_selector('a.ellipsis'):
print(title.get_attribute('title'))
data.append(title.get_attribute('title'))
driver.close()
result = {
'data': data
}
return json.dumps(result, ensure_ascii=False)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()