标签:turn .text *** err fine att awb encoding rom
re.findall
soup.find_all
---------Q----
for i in ***:
***可以是什么类型,主要是关心什么类型的不可以
------------trackback用法------------
>>>
>>> import traceback
>>> try:
... 1/0
... except (Exception,e):
... traceback.print_exc()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name ‘e‘ is not defined
(说是这样报错更加直观
http://blog.csdn.net/handsomekang/article/details/9373035
--------------有毛病,睡了--------------
- import requests
- from bs4 import BeautifulSoup
- import traceback
- import re
-
- def getHTMLText(url, code="utf-8"):
- try:
- r = requests.get(url)
- r.raise_for_status()
- r.encoding = code
- return r.text
- except:
- return ""
-
- def getStockList(lst, stockURL):
- html = getHTMLText(stockURL, "GB2312")
- soup = BeautifulSoup(html, ‘html.parser‘)
- a = soup.find_all(‘a‘)
- for i in a:
- try:
- href = i.attrs[‘href‘]
- lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
- except:
- continue
-
- def getStockInfo(lst, stockURL, fpath):
- count = 0
- for stock in lst:
- url = stockURL + stock + ".html"
- html = getHTMLText(url)
- try:
- if html=="":
- continue
- infoDict = {}
- soup = BeautifulSoup(html, ‘html.parser‘)
- stockInfo = soup.find(‘div‘,attrs={‘class‘:‘stock-bets‘})
-
- name = stockInfo.find_all(attrs={‘class‘:‘bets-name‘})[0]
- infoDict.update({‘股票名称‘: name.text.split()[0]})
-
- keyList = stockInfo.find_all(‘dt‘)
- valueList = stockInfo.find_all(‘dd‘)
- for i in range(len(keyList)):
- key = keyList[i].text
- val = valueList[i].text
- infoDict[key] = val
-
- with open(fpath, ‘a‘, encoding=‘utf-8‘) as f:
- f.write( str(infoDict) + ‘\n‘ )
- count = count + 1
- print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="")
- except:
- count = count + 1
- print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="")
- continue
-
- def main():
- stock_list_url = ‘http://quote.eastmoney.com/stocklist.html‘
- stock_info_url = ‘https://gupiao.baidu.com/stock/‘
- output_file = ‘D:/BaiduStockInfo.txt‘
- slist=[]
- getStockList(slist, stock_list_url)
- getStockInfo(slist, stock_info_url, output_file)
-
- main()
- ---------------------------------------------
- import requests
- from bs4 import BeautifulSoup
- import traceback
- import re
-
- def getHTMLText(url):
- try:
- r = requests.get(url)
- r.raise_for_status()
- r.encoding = r.apparent_encoding
- return r.text
- except:
- return ""
-
- def getStockList(lst, stockURL):
- html = getHTMLText(stockURL)
- soup = BeautifulSoup(html, ‘html.parser‘)
- a = soup.find_all(‘a‘)
- for i in a:
- try:
- href = i.attrs[‘href‘]
- lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
- except:
- continue
-
- def getStockInfo(lst, stockURL, fpath):
- for stock in lst:
- url = stockURL + stock + ".html"
- html = getHTMLText(url)
- try:
- if html=="":
- continue
- infoDict = {}
- soup = BeautifulSoup(html, ‘html.parser‘)
- stockInfo = soup.find(‘div‘,attrs={‘class‘:‘stock-bets‘})
-
- name = stockInfo.find_all(attrs={‘class‘:‘bets-name‘})[0]
- infoDict.update({‘股票名称‘: name.text.split()[0]})
-
- keyList = stockInfo.find_all(‘dt‘)
- valueList = stockInfo.find_all(‘dd‘)
- for i in range(len(keyList)):
- key = keyList[i].text
- val = valueList[i].text
- infoDict[key] = val
-
- with open(fpath, ‘a‘, encoding=‘utf-8‘) as f:
- f.write( str(infoDict) + ‘\n‘ )
- except:
- traceback.print_exc()
- continue
-
- def main():
- stock_list_url = ‘http://quote.eastmoney.com/stocklist.html‘
- stock_info_url = ‘https://gupiao.baidu.com/stock/‘
- output_file = ‘D:/BaiduStockInfo.txt‘
- slist=[]
- getStockList(slist, stock_list_url)
- getStockInfo(slist, stock_info_url, output_file)
-
- main()
- -------------------学校这破网,让我开始怀疑人生了-------吃屎,强力吃屎,学校吃屎了?ta为什么要吃屎呢?-------
python-股票数据定向爬取
标签:turn .text *** err fine att awb encoding rom
原文地址:http://www.cnblogs.com/wanghui626/p/6664242.html