python3 BeautifulSoup爬去股票信息

爬网页信息用到了BeautifulSoup这个,通过这个库能很好的解析网页信息,可以从标签 css等方式取信息 .

import requests
from bs4 import BeautifulSoup


headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
reponse = requests.get('http://quote.stockstar.com/stock/gem.shtml')
reponse.encoding = 'GBK'
soup = BeautifulSoup(reponse.text,'lxml')
trs = soup.select('tbody.tbody_right')[0].select('tr')
for i in trs:
    print(i.text)
print('*'*50)
datalist = soup.find_all(id='datalist')
for a in datalist[0].select('tr'):
    print(a.text)
python3 BeautifulSoup爬去股票信息_第1张图片
BaiduShurufa_2019-1-20_22-16-4.png

你可能感兴趣的:(python3 BeautifulSoup爬去股票信息)