python爬虫 获取华为应用商店app信息

最近项目需要几个品牌手机应用商店上app的一些数据,所以就上网找了一些爬虫的文章,一边学一边做,把方法记录下来,方便以后查看。
代码具体功能是爬取华为应用商店上app的数据,并把数据放到一个csv文件上。
当时参考的文章:

https://blog.csdn.net/datacastle/article/details/78812363
基本是按照上面链接中文章的方法去做的

在这里插入图片描述主要爬取游戏和软件两个种类的app,都选取了更新时间这个排序,因为这个排序下面app的数量是最多的。

在这里插入图片描述 href_list=s.xpath(’/html/body/div[1]/div[4]/div[1]/div[2]/div[2]/div/div[2]/h4/a/@href’)

应用名称 获取方法如下,其他的几个信息如此类推:
在这里插入图片描述name=s2.xpath(’//*[@id=“bodyonline”]/div/div[4]/div[1]/div/div/div[1]/ul[1]/li[2]/p[1]/span[1]/text()’)[0]

app的下载地址获取方法比较特别,
在这里插入图片描述infor=s2.xpath(’//*[@id=“bodyonline”]/div/div[4]/div[1]/div/div/div[2]/a/@onclick’)[0]
#用re.findall把下载地址取出来
downaddr=re.findall(r"[’]",infor)[5]#取出下载地址

具体代码如下,把写入文件的路径修改成自己的路径就可以直接使用

import urllib.request
from lxml import etree
import re
import datetime

with open('D:/software_file/pythonFile/huawei.csv','w',encoding='gb18030') as f:
    f.write("{},{},{},{},{},{},{},{},{},{},{},{}\n".format('应用名称','应用类型','公司名称','app大小','版本号','更新时间','评分人数','下载人数',\
                                                  '应用介绍','图标','下载地址','爬取时间'))
    for dev in range(2,14,11):
        for page in range(1,10):
            if dev==2:
                devType='game'
            if dev==13:
                devtype='soft'
            url ="http://app.hicloud.com/{}/list_{}_1_{}".format(devType,dev,page)
            headers = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
            #直接访问应用市场网址会提示403错误
            #需要模拟浏览器访问,解决403错误
            opener = urllib.request.build_opener()
            opener.addheaders = [headers]
            data = opener.open(url).read()
            s=etree.HTML(data)
            href_list=s.xpath('/html/body/div[1]/div[4]/div[1]/div[2]/div[2]/div/div[2]/h4/a/@href')

            for nhref in href_list:
                url2="http://app.hicloud.com"+nhref
                data2 = opener.open(url2).read()
                s2=etree.HTML(data2)
                try:
                    name=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[1]/li[2]/p[1]/span[1]/text()')[0]
                    size=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[2]/li[1]/span/text()')[0]
                    updataTime=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[2]/li[2]/span/text()')[0]
                    company=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[2]/li[3]/span/@title')[0]
                    version=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[2]/li[4]/span/text()')[0]
                    downloadNum=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[1]/li[2]/p[1]/span[2]/text()')[0]
                    strNum=downloadNum.lstrip('下载:')
                    introduct=s2.xpath('//*[@id="app_strdesc"]/text()')[0]
                    # csv是用英文逗号来区分一列的,所以如果应用介绍中有英文逗号需要替换成空格,要不然应用介绍会分成好几列
                    if ',' in introduct:
                        introduct=introduct.replace(',','  ')
                    updataTime=updataTime.strip()#去掉字符串前后空格
                    company=company.strip()#去掉字符串前后空格
                    version=version.strip()#去掉字符串前后空格
                    strNum=strNum.strip()#去掉字符串前后空格
                    introduct=introduct.strip()#去掉字符串前后空格
                    picture=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[1]/ul[1]/li[1]/img/@src')[0]              
                    infor=s2.xpath('//*[@id="bodyonline"]/div/div[4]/div[1]/div/div/div[2]/a/@onclick')[0]
                    downaddr=re.findall(r"['](.*?)[']",infor)[5]#取出下载地址                                                     
                    currTime=datetime.datetime.now()
                    f.write('{},{},{},{},{},{},{},{},{},{},{},{}'.format(name,'',company,size,version,updataTime,'',
                                                                   strNum,introduct,picture,downaddr,currTime))
                    f.write('{}'.format('\n'))

                except IndexError:#出现异常跳出,防止程序崩溃
                    pass
            print("{},{}".format(dev,page))

生成的表格效果图如下,大概400个app,3分钟左右就爬取完成。
在这里插入图片描述

后面还做了小米和魅族应用市场的app爬虫,等有时间再把更新上来吧

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值