鱼C论坛

 找回密码
 立即注册
查看: 1588|回复: 2

想用xlsxwriter把数据存进xls表格

[复制链接]
发表于 2020-9-4 09:16:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import re,requests
import time,json
from pyquery import PyQuery as pq
import xlsxwriter
from snownlp import SnowNLP

mybook = xlsxwriter.Workbook("1.xls")#建立文件
worksheet = mybook.add_worksheet()

headers = {
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh-CN,zh;q=0.9",
    "content-length": "278",
    "content-type": "application/json",
    "cookie":"_abtest_userid=0f540837-6c5a-4629-aa88-93ba1bfec570; _RSG=pHqDcQKOqkD92.7L6HsBL8; _RDG=28b4f4d49352f22e56153739a68cf82eae; _RGUID=dbb46146-a637-4473-854b-d911951fe089; MKT_CKID=1591713956511.w9k7w.9opk; _ga=GA1.2.1020029856.1591713957; _RF1=119.39.127.107; Session=smartlinkcode=U130709&smartlinklanguage=zh&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=; Union=AllianceID=4901&SID=130709&OUID=&createtime=1599095310&Expires=1599700110468; MKT_CKID_LMT=1599095310492; MKT_Pagesource=PC; nfes_isSupportWebP=1; _jzqco=%7C%7C%7C%7C1599095310630%7C1.1301033936.1591713956505.1599095310501.1599095863824.1599095310501.1599095863824.0.0.0.3.3; __zpspc=9.2.1599095310.1599095863.2%233%7Cwww.sogou.com%7C%7C%7C%7C%23; _bfi=p1%3D290510%26p2%3D290510%26v1%3D4%26v2%3D3; _bfa=1.1591713953612.3xeul0.1.1591713953612.1599095307538.2.5; _bfs=1.4",
    "cookieorigin":"https://you.ctrip.com",
    "origin": "https://you.ctrip.com",
    "referer": "https://you.ctrip.com/sight/lijiang32/3049.html",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
    }

row = 0
col = 0
data = {"_fxpcqlniredt": "09031090311113380393"}
payload = {
        "arg":{"channelType": "2" ,                   
        "collapseType": "0",
        "commentTagId": "0",
        "pageIndex":"1",
        "pageSize": "10",
        "poiId": "75919",
        "sortType":" 3",
        "sourceType": "1",
        "starType": "0"},        
        }
url = "https://m.ctrip.com/restapi/soa2/13444/json/getCommentCollapseList?_fxpcqlniredt=09031090311113380393"
data = requests.post(url,headers = headers, params = data, data = json.dumps(payload),verify = False)
data.encoding = "utf-8" 
resove_college_url = data.text
pat1 = '"content":"(.*?)",'
rest1 = re.compile(pat1,re.S).findall(resove_college_url)

for j in range(0,len(rest1)):
    print(rest1[j].encode("gbk","ignore").decode("gbk"))
    s = SnowNLP(rest1[j])
    print(s.sentiments)
    worksheet.write(row,col,rest1[j])
    worksheet.write(row,col+1,s.sentiments)

运行后xls表格中没有存贮进数据,请问是哪里出错误了吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-4 09:39:35 | 显示全部楼层
你定义的row和col一直都是0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-4 10:44:23 | 显示全部楼层
天边一抹灬红 发表于 2020-9-4 09:39
你定义的row和col一直都是0

我再后面加上row = row +1,col = col + 1,但是还是没有导入成功
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-18 20:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表