Python作业2:使用LXML方法爬取数据

import requests

from lxml import etree

import xlwt

all_info_list=[]

def get_info(url):

      res = requests.get(url)

      html = etree.HTML(res.text)

      infos = html.xpath('//ul[@class="rank_list"]/li')

      for infoin infos:

           rank1 = info.xpath('em ')[0]

           rank = rank1.xpath('string(.)').strip()

           name = info.xpath('a/text()')[0]

           size = info.xpath('i/text()')[0]

           popularity = info.xpath('span/text()')[0]

           # print(game,size,popularity)

           info_list = [rank,name,size,popularity]

           all_info_list.append(info_list)

if __name__ =='__main__':

      book = xlwt.Workbook(encoding='utf-8')

      sheet = book.add_sheet('Sheet1')

      header = ['rank','name','size','popularity']

      for t  in range(len(header)):

            sheet.write(0, t, header[t])

      urls = ['http://www.doyo.cn/rank/shouji/100/page/{}'.format(str(i))for i  in range(1,4)]

      for url in urls:

              get_info(url)

       i =1

       for list in all_info_list:

             j =0

             for datain list:

                    sheet.write(i, j, data)

                     j +=1

              i +=1

    book.save('C:/Users/Xumeiling/Desktop/手机游戏排行榜.xls')


你可能感兴趣的:(Python作业2:使用LXML方法爬取数据)