如何用python爬取某东评论-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

如何用python爬取某东评论

今天小编给大家分享一下如何用python爬取某东评论的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

10年积累的网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有赫章免费网站建设让你可以放心的选择与我们合作。

import requestsimport jsonimport osimport timeimport randomimport jiebafrom wordcloud import WordCloudfrom imageio import imread
comment_file_path = 'jd_comments.txt'
def get_spider_comments(page = 0):
   #爬取某东评论    url = 'https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv7990&productId=1070129528&score=0&sortType=5&page=%s&pageSize=10&isShadowSku=0&rid=0&fold=1'%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',        'referer':'https://item.jd.com/1070129528.html'    }    try:        response = requests.get(url, headers = headers)    except:        print("something wrong!")    #获取json格式数据集    comments_json = response.text[26:-2]    #将json数据集转为json对象    comments_json_obj = json.loads(comments_json)    #获取comments里面的所有内容    comments_all = comments_json_obj['comments']    #获取comments中评论content的内容    for comment in comments_all:        with open(comment_file_path,'a+' ,encoding='utf-8') as fin:            fin.write(comment['content']+'\n')        print(comment['content'])
def batch_spider_comments():    # 每次写入数据之前先清空文件    if os.path.exists(comment_file_path):        os.remove(comment_file_path)    for i in range(100):        print('正在爬取'+str(i+1)+'页数据。。。。')        get_spider_comments(i)        time.sleep(random.random()*5)
def cut_word():    with open(comment_file_path,encoding='utf-8')as file:        comment_text = file.read()        wordlist = jieba.lcut_for_search(comment_text)        new_wordlist = ' '.join(wordlist)        return new_wordlist
def create_word_cloud():    mask = imread('ball.jpg')    wordcloud = WordCloud(font_path='msyh.ttc',mask = mask).generate(cut_word())    wordcloud.to_file('picture.png')
if __name__ == '__main__':        create_word_cloud()

以上就是“如何用python爬取某东评论”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注创新互联行业资讯频道。


本文标题:如何用python爬取某东评论
标题路径:http://kswsj.cn/article/pjchoo.html

其他资讯