# -*-coding:utf8-*- import requests import re import math j = 1 #查看每一页都多少个APP,用于决定我们要翻多少页 html = requests.get('http://app.mi.com/topList')#获取网页的源代码 label = re.findall('<p class="app-desc">', html.text, re.S)#打开网页源码发现每个App名称前面有<p class="app-desc">, # 所有统计这个标示的个数就可以知道每一页有多少个APP啦 pageAppNum = len(label)#label的长度就代表了每一页APP的个数 # print pageAppNum print(u'请输入App的数目:') appNum = int(input()) #根据要爬取的APP个数决定要翻多少页 pageNum = math.ceil(float(appNum) / pageAppNum) for i in range(1, int(pageNum + 1)): #html = requests.get('http://app.mi.com/topList?page=' + str(i))#括号内是每一页的连接地址 #http://app.mi.com/category/1#page=0 html = requests.get('http://app.mi.com/category/1#page=' + str(i)) print(i) # print html.text title = re.findall('</a><h5><a href=".*?">(.*?)</a></h5><p class="app-desc">', html.text, re.S)#找对应的APP的名称在网页中的位置,并用正则表达式来获取,如果看不懂,那你该好好去看看正则表达式了 Lab = re.findall('</h5><p class="app-desc"><a href=".*?">(.*?)</a></p>', html.text, re.S) #将每一页的APP按先后顺序输出,知道第100个 List_title = [] for title in title: if j <= appNum: List_title.append(title) j = j + 1 List_Lab = [] for title in title: if j <= appNum: List_Lab.append(title) j = j + 1 ########################## print(List_title)
爬取小米应用商城
最新推荐文章于 2022-03-29 10:41:34 发布