加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

抓取期货咨询供分析

发布时间:2020-05-24 23:40:37 所属栏目:Python 来源:互联网
导读:抓取期货咨询供分析

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

# encoding:utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf8")
import urllib,urllib2
from pprint import pprint
from collections import Counter,OrderedDict
from bs4 import BeautifulSoup
from jinja2 import Environment,PackageLoader


class HexunQihuo(object):
    def __init__(self):
        self.all_href = []

    def __get_max_index(self,soup):
        listdh = soup.find('div',class_='listdh')
        s1 = str(listdh).split('maxPage = ')[1]
        s2 = s1.split(';')[0]
        return int(s2)

    def fetch_data(self,qtype,qname):
        '''
          qtype is in [industrynews,agriculturenews,nyzx] 
        # http://futures.hexun.com/industrynews/index.html') # 金属
        # http://futures.hexun.com/agriculturenews/index.html') # 农副资讯 
        # http://futures.hexun.com/nyzx/index.html  # 能源资讯
        '''
        self.qtype = qtype
        self.qname = qname
        f = urllib.urlopen('http://futures.hexun.com/%s/index.html' % qtype) #
        soup = BeautifulSoup(f.read(),'html.parser')
        keywd = qname.decode('utf-8')
        temp01 = soup.find('div',class_='temp01')
        content = temp01.select('li > a')
        hrefs = [a['href'] +' '+ a.string for a in content if keywd in a.string]
        self.all_href.extend(hrefs)
        max_idx = self.__get_max_index(soup)

        do_range = range(670,max_idx)[::-1]
        for idx in do_range:
            print idx
            f = urllib.urlopen('http://futures.hexun.com/%s/index-%s.html' % (qtype,idx))
            soup = BeautifulSoup(f.read(),'html.parser')
            temp01 = soup.find('div',class_='temp01')
            content = temp01.select('li > a')
            hrefs = [a['href'] +' '+ a.string for a in content if keywd in a.string]
            self.all_href.extend(hrefs)
        self.all_hrefstr = 'n'.join(self.all_href)
        return self

    def write_txt(self):
        with open('file.txt','w') as ff:
            ff.write(str(self.all_hrefstr))

    def write_html(self):
        # hexunqihuo 是py的文件名  在同级目录下建个templates文件夹
        env = Environment(loader=PackageLoader('hexunqihuo','templates'))
        data = OrderedDict()
        for href in self.all_href:
            item = href.split(' ')
            data[item[0]] = item[1]
        template = env.get_template('template.html') # templates/template.html
        html = template.render(data=data)
        htmlfile = 'files/%s.html' % self.qname
        with open(htmlfile,'w') as ff:
            ff.write(html)


if __name__ == '__main__':

    HexunQihuo().fetch_data('industrynews',u'螺纹钢').write_html()

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读