-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatch.py
52 lines (40 loc) · 987 Bytes
/
catch.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
import sys
import urllib
import urllib2
import xlwt
from bs4 import BeautifulSoup
stock = sys.argv[1]
begin = sys.argv[2]
end = sys.argv[3]
wb = xlwt.Workbook()
ws = wb.add_sheet('a test sheet')
url = "http://" + stock + ".stock.inv.org.cn/quote/history.php"
values = {
"type": "daily",
"begin_day": begin,
"end_day": end
}
data = urllib.urlencode(values)
headers={'User-Agent': "Mozilla/5.0"}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
page = response.read()
soup = BeautifulSoup(page)
table = soup.table
rows = table.findAll("tr")
x = 0
for tr in rows:
cols = tr.findAll("td")
if not cols:
continue
y = 0
for td in cols:
text_bu = td.text
text_bu = text_bu.encode("UTF-8")
text_bu = text_bu.strip()
ws.write(x, y, td.text)
#print(x, y, td.text)
y = y + 1
x = x + 1
filename = stock + "_" + begin + "_" + end +".xls"
wb.save(filename)