-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
48 lines (40 loc) · 1.19 KB
/
app.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
import os
import pickle
import subprocess
from pathlib import Path
from urllib.parse import urlparse
from flask import Flask, render_template, request
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
output = Path('./output')
app = Flask(__name__)
def decide_spider(url):
domains = {
'buy.housefun.com.tw': 'housefun',
'sale.591.com.tw': '591',
'www.591.com.tw': '591',
'buy.yungching.com.tw': 'yungching',
}
try:
return domains[urlparse(url).netloc]
except KeyError:
return None
@app.route('/')
def index():
return render_template('index.html')
@app.route('/crawl', methods=['POST'])
def crawl_591():
if request.method == 'POST':
url = request.form["url"]
spider = decide_spider(url)
if not spider:
return "目前只支援591跟好房網"
if output.exists():
os.remove(str(output))
subprocess.run(
['scrapy', 'crawl', spider, '-a', f'url={url}'],
)
with output.open('rb') as f:
data = pickle.load(f)
data['url'] = url
return render_template('results.html', data=data)