forked from queensun/Nyspider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_finance.py
67 lines (62 loc) · 2.16 KB
/
new_finance.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
#coding:utf-8
import requests
from bs4 import BeautifulSoup
import xlwt3
import xlrd
import threading
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0"}
class Get_infor(threading.Thread):
def __init__(self,name):
super(Get_infor,self).__init__()
self.name=name
self.url='http://finance.yahoo.com/q?s='+name
def run(self):
try:
html=requests.get(self.url,headers=headers,timeout=30).text
table=BeautifulSoup(html).find('table',id='table1').find_all('tr')
self.infor={}
for item in table:
if item.find('th').get_text()=='Beta:':
self.infor['Beta']=item.find('td').get_text()
table=BeautifulSoup(html).find('table',id='table2').find_all('tr')
for item in table:
if item.find('th').get_text()[0]=='P':
self.infor['PE']=item.find('td').get_text()
except:
self.infor={}
self.infor['Beta']=''
self.infor['PE']=''
class Main():
def __init__(self):
self.f=xlwt3.Workbook()
self.sheet=self.f.add_sheet('sheet')
self.count=0
def run(self):
file=open('data.txt','r')
items=[]
for line in file:
data=line.strip().split(' ')
items.append(data[1])
if len(items)<10:
continue
threadings=[]
for item in items:
work=Get_infor(item)
threadings.append(work)
for work in threadings:
work.run()
for work in threadings:
self.sheet.write(self.count,0,work.name)
self.sheet.write(self.count,1,work.infor['Beta'])
self.sheet.write(self.count,2,work.infor['PE'])
self.count+=1
self.f.save('data2.xls')
items=[]
print(self.count)
work=Main()
work.run()