python爬虫实训第五天

京东评论爬虫数据库版

(1) 京东评论爬虫数据库版

import  json
import sqlite3
import requests
import time
def get_one(pid,pageno):
    #取一个商品的一页评论

    base_url = 'https://club.jd.com/comment/productPageComments.action'
#本次请求头只要伪造请求头User_Agent,但前端时间测试需要cookie字段
    headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'

}
#Referer: https://item.jd.com/,反爬虫可能用到,从哪里来的
    params = {

    'productId': pid,#商品id
    'score': 0,
    'sortType': 5,
    'page': pageno,#第n页
    'pageSize': 10,
    'isShadowSku': 0,
    'rid': 0,
    'fold': 1
    }
    resp = requests.get(base_url,headers=headers,params=params)
    comments_json = resp.text
    #(comments_json)
#京东评论接口返回jsonp 涉及跨域问题,需要将jsonp转化为json
#方法一:python字符串方式删除  2、3、本例中发现删掉第一个阐述,就可以返回完美的json
    comments_obj = json.loads(comments_json)
    comments = comments_obj['comments']
    #print(comments)
    return comments
def write_comments_to_db(c,cursor):
        cid = c['id']
        content = c['content']
        creation_time = c['creationTime']
        product_color = c['productColor']
        #product_size = c['productSize']
        cursor.execute("""
        insert into contents(cid,content,product_color,creation_time) values(?,?,?,?);
        """, [cid,content,product_color,creation_time])
if __name__=='__main__':
    connect = sqlite3.connect('./jd.db')
    cursor = connect.cursor()
    cursor.execute("""
    create table if not exists contents(
    id integer primary key,
    cid integer,
    content Text,
    product_color TEXT,
    creation_time DATETIME);
    """)
    product_id = '100009077475'
    for pageno in range(1,100):
        one_page_comments = get_one(product_id,pageno)
        for c in one_page_comments:
            write_comments_to_db(c,cursor)
        connect.commit()
        print(f'第{pageno}页数据插入完成')
        time.sleep(1)
    connect.close()

主要困难及问题:
今天在昨天的基础上学习了如何通过datagrip将在京东评论区获取的数据存入数据库中,使用sqlite3包创建和数据库的连接,然后使用cursor的execute方法来实现数据库的插入操作,最后记得用commit提交。此时获取的数据需要进行分析才能分析出产品的好坏,在使用jieba分词之后,需要去除数据中的无意义的停止词,用python的in来处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值