小小鹏 发表于 2020-9-4 09:16:02

想用xlsxwriter把数据存进xls表格

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.encode("gbk","ignore").decode("gbk"))
    s = SnowNLP(rest1)
    print(s.sentiments)
    worksheet.write(row,col,rest1)
    worksheet.write(row,col+1,s.sentiments)





运行后xls表格中没有存贮进数据,请问是哪里出错误了吗?

天边一抹灬红 发表于 2020-9-4 09:39:35

你定义的row和col一直都是0

小小鹏 发表于 2020-9-4 10:44:23

天边一抹灬红 发表于 2020-9-4 09:39
你定义的row和col一直都是0

我再后面加上row = row +1,col = col + 1,但是还是没有导入成功
页: [1]
查看完整版本: 想用xlsxwriter把数据存进xls表格