本次目的
python批量采集某商品数据
知识点:
requests 发送请求
re 解析网页数据
json 类型数据提取
csv 表格数据保存
开发环境:
python 3.8
pycharm
requests
代码
导入模块
import json
import random
import time
import csv
import requests
import re
import pymysql
# 连接数据库
def save_sql(title, pic_url, detail_url, view_price, item_loc, view_sales, nick):
count = pymysql.connect(
host='xxx.xxx.xxx.xxx', # 数据库地址
port=3306, # 数据库端口
user='xxxx', # 数据库账号
password='xxxx', # 数据库密码
db='xxxx' # 数据库表名
)
# 创建数据库对象
db = count.cursor()
# 写入sql
sql = f"insert into goods(title, pic_url, detail_url, view_price, item_loc, view_sales, nick) values ('{
title}', '{
pic_url}', '{
detail_url}', {
view_price}, '