博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
评论抓取:Python爬取微信在APPStore上的评论内容及星级
阅读量:4550 次
发布时间:2019-06-08

本文共 3236 字,大约阅读时间需要 10 分钟。

#完整程序如下:import requestsimport redef getHTMLText(url):    try:        r = requests.get(url)        r.raise_for_status()        r.encoding = r.apparent_encoding        return r.text    except:        return ''def printAPPName(html):    try:        pattern = re.compile(r'{"im:name":{"label":(.*?)}, "rights"', re.S)        #如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始,不会跨行。        #而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,将“\n”当做一个普通的字符加入到这个字符串中,在整体中进行匹配        APPName = re.findall(pattern, str(html))        return 'APPName:' + str(APPName)    except:        return ''def fillUnivlist(titles, comments, stars, html):    try:        pattern = re.compile(r'"title":{"label":(.*?)}, "content"', re.S) #提取标题        nbaInfo = re.findall(pattern, str(html)) #提取title        # findStr = '"title":{"label":'        # nbaInfo = nbaInfo1[nbaInfo1.find(findStr)+len(findStr):]        patternFloor = re.compile(r'"content":{"label":(.*?), "attributes":{"type":"text"}}', re.S) #提取content        floorText = re.findall(patternFloor, str(html))        patternStar = re.compile(r'"im:rating":{"label":(.*?)}, "id"', re.S)  # 提取星级        star = re.findall(patternStar, str(html))        # print(str(star))        number = len(nbaInfo)        print(number)        for i in range(number):            Info = nbaInfo[i] #利用Tools类移除不想要的格式字符            if i==0:Info = Info[Info.find('"title":{"label":')+len('"title":{"label":'):]            # print(Info)            Info1 = floorText[i]            Info2 = star[i]            # print(Info2+"hello")            titles.append('title:' + Info)            comments.append('content:' + Info1)            stars.append('star:' + Info2)    except:        return ''def writeText(titleText, fpath):    try:        with open(fpath, 'a', encoding='utf-8') as f:            f.write(str(titleText)+'\n')            f.write('\n')            f.close()    except:        return ''def writeUnivlist(titles, comments, stars, fpath, num):    with open(fpath, 'a', encoding='utf-8') as f:        for i in range(num):            f.write(str(stars[i]) + '\n')            f.write('*' * 10 + '\n')            f.write(str(titles[i]) + '\n')            f.write('*' * 50 + '\n') #输入一行*号            f.write(str(comments[i]) + '\n')            f.write('*' * 100 + '\n')        f.close()def main():    count = 0    url = 'https://itunes.apple.com/rss/customerreviews/page=1/id=414478124/sortby=mostrecent/json?l=en&&cc=cn' #要访问的网址    output_file = 'D:/StockInfo.txt' #最终文本输出的文件    html = getHTMLText(url) #获取HTML    APPName = printAPPName(html)    writeText(APPName, output_file)    for i in range(10):        i = i + 1        titles = []        comments = []        stars = []        url = 'https://itunes.apple.com/rss/customerreviews/page=' + str(i) + '/id=414478124/sortby=mostrecent/json?l=en&&cc=cn'        html = getHTMLText(url)        fillUnivlist(titles, comments, stars, html)        writeUnivlist(titles, comments, stars, output_file, len(titles))        count = count + 1        print("\r当前进度: {:.2f}%".format(count * 100 / 10), end="")if __name__ == '__main__':    main()#如果想爬取其他APP只需要改变id的值,如想爬腾讯的,只需将id=414478124换成id=444934666#另外本程序是模仿https://www.cnblogs.com/sea-ocean/p/6601421.html的

 

转载于:https://www.cnblogs.com/wu-chao/p/9210416.html

你可能感兴趣的文章
poj2417 bzoj3239 Discrete Logging(bsgs)
查看>>
UVa10054 - The Necklace(欧拉回路【输出带来的麻烦)
查看>>
string和stringbuffer的区别 集合的作用 ArrayList vector linklist hashmap hashtable collection和collections...
查看>>
6月27日 ajax
查看>>
iOS开发之画图板(贝塞尔曲线)
查看>>
4嵌入式作业io
查看>>
IntelliJ Idea编译报错:javacTask: 源发行版 1.7 需要目标发行版 1.7
查看>>
Cognos中新建SQLserver数据源的步骤
查看>>
HttpClient连接超时及读取超时
查看>>
SQL优化方法
查看>>
SEO必须掌握的高级搜索指令
查看>>
生产者消费者模型
查看>>
ORACLE 字符串超长问题解决方案
查看>>
使用ZooKeeper协调多台Web Server的定时任务处理(方案1)
查看>>
20171116 每周例行报告
查看>>
[C#] SHA1校验函数用法
查看>>
linux 下 VMware 提示Unable to change virtual machine power state:
查看>>
洛谷P1585 魔法阵
查看>>
线程 题待做
查看>>
PL/SQL可以连oracle,但是jdbc连不上 【转】
查看>>