python爬取steam市场数据

  • 用python写了个爬虫,爬取csgo中AK47红线的交易数据并保存python爬取steam市场数据_第1张图片
import csv
import requests
import re

url='https://steamcommunity.com/market/listings/730/AK-47%20%7C%20Redline%20%28Field-Tested%29'
headers={
            'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36',
            }

response=requests.get(url,headers=headers)

result=re.search('',response.text,re.S)  
#匹配表格的信息,价格为美元,返回的类型为字符串
path=r'C:\Users\Administrator\Desktop\python\ak47list.txt'

with open(path,'w',encoding='utf-8') as f:
    f.write(result.group(1))
    
file=open(path)
file_read=file.read()

table=str.maketrans('','','":+[]')#删除字符串中的“ : + []
file_translate=file_read.translate(table)

lst=file_translate.split(',')#以逗号为分隔符,将字符串转换为列表

list_time=[]
list_price=[]
list_num=[]
#将时间、价格、数量三个信息分别存入三个list
i=0
j=0
while i < len(lst):       
    list_time.insert(j,lst[i])
    list_price.insert(j,lst[i+1])
    list_num.insert(j,lst[i+2])
    i=i+3
    j=j+1
#创建csv文件,并将数据写入
with open('ak47list.csv','a') as csvfile:
   writer=csv.writer(csvfile)
   writer.writerow(['time','price','number'])
   for i in range(len(list_time)):
       writer.writerow([list_time[i],list_price[i],list_num[i]])

你可能感兴趣的:(python爬取steam市场数据)