import urllib.request import urllib.parse from bs4 import BeautifulSoup from day04.Item import Stock url = 'http://quote.stockstar.com/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36' } request = urllib.request.Request(url=url,headers=headers) response = urllib.request.urlopen(request) content = response.read().decode('gb2312') soup = BeautifulSoup(content,'lxml') tr_list = soup.select('#datalist > tr ') stocks = [] for tr in tr_list: code = tr.find_all('td')[0].get_text() name = tr.find_all('td')[1].get_text() price = tr.find_all('td')[2].get_text() stock = Stock(code,name,price) stocks.append(stock.__dict__) import json str1 = json.dumps(stocks, ensure_ascii=False) with open('stocks.json','w',encoding='utf-8')as fp: fp.write(str1) # print(a.get_text()) # b = a.select('a') # print(a.select('a')) # print(a) # print(type(a)) # print(a.get_text())
python爬虫bs4抓取股票信息
最新推荐文章于 2024-04-10 08:45:00 发布