第二次大作业

作业一

  • 要求:在中国气象网(http://www.weather.com.cn)给定城市集的7日天气预报,并保存在数据库。

  • 输出信息:

    序号 地区 日期 天气信息 温度
    1 北京 7日(今天) 晴间多云,北部山区有阵雨或雷阵雨转晴转多云 31℃/17℃
    2 北京 8日(明天) 多云转晴,北部地区有分散阵雨或雷阵雨转晴 34℃/20℃
    3 北京 9日(后台) 晴转多云 36℃/22℃
    4 北京 10日(周六) 阴转阵雨 30℃/19℃
    5 北京 11日(周日) 阵雨 27℃/18℃
    6......
  • 完成过程

  • 本题是代码复现,因此学习点着重于理解代码,本题使用到了数据库存储

    def openDB(self):
        self.con=sqlite3.connect("weathers.db")
        self.cursor=self.con.cursor()
        try:
            self.cursor.execute("create table weathers (wCity varchar(16),wDate varchar(16),wWeather varchar(64),wTemp varchar(32),constraint pk_weather primary key (wCity,wDate))")
        except:
            self.cursor.execute("delete from weathers")

    def closeDB(self):
        self.con.commit()
        self.con.close()

    def process(self, cities):
            self.db = WeatherDB()  # 创建天气数据库对象,db
            self.db.openDB()  # 打开数据库
            for city in cities:
                self.forecastCity(city)  # 循环遍历,逐一爬取和存储天气预报数据
            self.db.show()#打印数据库中数据
            self.db.closeDB()  # 关闭数据库

作业二

  • 要求:用requests和BeautifulSoup库方法定向爬取股票相关信息。

  • 候选网站:东方财富网:http://quote.eastmoney.com/center/gridlist.html#hs_a_board

  • 技巧:在谷歌浏览器中进入F12调试模式进行抓包,查找股票列表加载使用的url,并分析api返回的值,并根据所要求的参数可适当更改api的请求参数。根据URL可观察请求的参数f1、f2可获取不同的数值,根据情况可删减请求的参数。
    *参考链接:https://zhuanlan.zhihu.com/p/50099084

  • 输出信息:

序号 股票代码 股票名称 最新报价 涨跌幅 涨跌额 成交量 成交额 振幅 最高 最低 今开 昨收
1 688093 N世华 28.47 62.22% 10.92 26.13万 7.6亿 22.34 32.0 28.08 30.2 17.55
2......
  • 解题过程
  • 1)首先对网页进行抓包,查找到股票列表加载使用的url
  • 2)根据题目要求选取f*
  • def getText(html):
        Shares = re.findall(r"\"diff\":\[(.*?)\]",html,re.M|re.S)
        Shares = list(eval(Shares[0]))
        global Num
        result = []  # 爬取结果存入列表
        for shares in Shares:
            sharesID = shares["f12"]
            SharesName = shares["f14"]
            Latestoffer = shares["f2"]
            Pricelimit = shares["f3"]
            RiseFall = shares["f4"]
            VolumeNum = shares["f5"]
            Voluem = shares["f6"]
            Amplitude = shares["f7"]
            Max = shares["f15"]
            Min = shares["f16"]
            TodayOpen = shares["f17"]
            YesterdayIncome = shares["f18"]
            result.append([Num, sharesID, SharesName, Latestoffer,Pricelimit, RiseFall, VolumeNum, Voluem, Amplitude, Max, Min, TodayOpen,YesterdayIncome])
            Num += 1
        return result
    
* 3)存储在数据库,首先要创建一个table包含所需属性
* ````
    def openDB(self):
        self.con = sqlite3.connect("shares.db")
        self.cursor = self.con.cursor()
        try:
            self.cursor.execute("create table shares (num varchar(64),SharesID varchar(32),Sharesname varchar(32),LatestOffer varchar(32),PriceLimit varchar(32),Risefall varchar(32),Volumenum varchar(32),voluem varchar(32),amplitude varchar(32),max varchar(32),min varchar(32),Todayopen varchar(32),Yesterdayincome varchar(32))")
        except:
            self.cursor.execute("delete from shares")
  • 接着向table中插入数据
  •  def insert(self,num,sharesID,SharesName,Latestoffer,Pricelimit,RiseFall,VolumeNum,Voluem,Amplitude,Max,Min,TodayOpen,YesterdayIncome):
            try:
                self.cursor.execute("insert into shares(num,SharesID,Sharesname,LatestOffer,PriceLimit,Risefall,Volumenum,voluem,amplitude,max,min,Todayopen,Yesterdayincome) values (?,?,?,?,?,?,?,?,?,?,?,?,?)",
                                    (num,sharesID,SharesName,Latestoffer,Pricelimit,RiseFall,VolumeNum,Voluem,Amplitude,Max,Min,TodayOpen,YesterdayIncome))
            except Exception as err:
                print(err)
    
* 最后以学号最后一位‘9’,爬取9页
* ````
  for page in range(1, 10):
      url = "http://19.push2.eastmoney.com/api/qt/clist/get?cb=jQuery11240009917002240502182_1634088844934&pn=" + str(page) + "&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1634088845178"
      html = getHtml(url)
      Shares = getText(html)
      for shares in Shares:
          print(s.format(shares[0], shares[1], shares[2], shares[3], shares[4], shares[5], shares[6], shares[7], shares[8], shares[9], shares[10], shares[11], shares[12], chr(12288)))
          sharesdb.insert(shares[0], shares[1], shares[2], shares[3], shares[4], shares[5], shares[6], shares[7], shares[8], shares[9], shares[10], shares[11], shares[12])

作业三

  • 要求: 爬取中国大学2021主榜 https://www.shanghairanking.cn/rankings/bcur/2021
    所有院校信息,并存储在数据库中,同时将浏览器F12调试分析的过程录制Gif加入至博客中。

  • 技巧: 分析该网站的发包情况,分析获取数据的api

  • 输出信息:

    排名 学校 总分
    1 清华大学 969.2
  • 解题步骤

  • 1)利用F12调试找到payload.js里的url,如下图

  • 发现url有一个字段会变化,需要更新url

  • 2)分析文本确定好正则表达式

  • 此文本解码后为

因此正则表达式为

     reg1 = 'univNameCn:"(.*?)"'
     reg2 = 'score:(.*?),'
     Uni_name = re.findall(reg1,html,re.S|re.M)
     Uni_score = re.findall(reg2,html,re.S|re.M)
posted @ 2021-10-26 01:24  施一念  阅读(48)  评论(0编辑  收藏  举报