爬取京東商品評論並存入資料庫(二)

2021-09-08 20:25:35 字數 2034 閱讀 4192

前言

上篇我詳細說明了爬取得過程,這裡就不過多解釋,直接上爬取得**,簡單明瞭:

**

import requests

import json

import pymysql

comment_url = ''

def jd(page):

params =

headers =

comment_resp = requests.get(url=comment_url, params=params,headers=headers)

comment_str = comment_resp.text

comment_dict = json.loads(comment_str)

comments = comment_dict['comments']

return comments

def jd_sql(page):

#儲存內容到mysql庫里

db = pymysql.connect(host='localhost',user='root',password='123456',db='jd',charset='utf8')

cursor = db.cursor()

sql_table = """create table if not exists jd_content(

id char(20) not null,

nickname char(20),

userlevelname char(20),

productcolor char(10),

productsize char(10),

salevalue char(10),

creationtime char(30),

content text,

usefulvotecount char(10),

replycount char(10)

)"""

cursor.execute(sql_table)

db.commit()

if page==0:

delete_table = """delete from jd_content"""

cursor.execute(delete_table)

db.commit()

#傳入值

comments = jd(page)

for comment in comments:

value = (

comment['id'], comment['nickname'], comment['userlevelname'], comment['productcolor'], comment['productsize'],

comment['productsales'][0]['salevalue'], comment['creationtime'], comment['content'],

comment['usefulvotecount'], comment['replycount'])

# print(value)

sql_insert = """insert into jd_content values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')""" % (value)

try:

# 執行sql語句

cursor.execute(sql_insert)

# 提交到資料庫執行

db.commit()

except:

# rollback in case there is any error

db.rollback()

cursor.close()

db.close()

for page in range(30):

print(f'正在爬取第頁···')

jd_sql(page)

print(f'爬取第頁成功···')

爬取京東某商品評論並存入csv

前言 找到你所要爬取物品的頁面,右鍵檢查,如圖所示 我們需要的就是紅方塊框柱的內容了。最後 我感覺可以直接上 了 import requests import json import csv from lxml import etree comment url for i in range 10 p...

用Python爬取京東商品評論(2)

首先開啟京東的頁面,輸入你想要查詢的某項商品的名稱,這裡用貝因美的某一款奶粉舉例 1.首先我們右擊選擇檢視網頁源 3.利用params引數構建 我們可以發現 callback fetchjson comment98vv75454 productid 1601354 score 0 sorttype ...

雪球網爬取資料並存入資料庫

from urllib import request import json import pymysql class mysql connect object 初始化的建構函式 def init self self.db pymysql.connect host 127.0.0.1 user ro...