柠檬冲冲冲 发表于 2022-2-14 11:12:35

':' expected错误

返回这样的错误,是什么原因,可以给我解释一下吗,谢谢大家

',' or ')' expected15行
':' expected 15行
':' expected16行
即下面这两行
for i in re.findall("<meta name="description"content="[\d\D]+。" />",html):
      for c in re.findall("t="[\d\D]+"",i):


import requests
import re
import jieba
import json

fg = 1
f = []
urll = "https://so.gushiwen.cn/gushi/tangshi.aspx"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"}
html = requests.get(urll, headers=headers).text
hh = re.findall("/shiwenv_[\d\D]{17}", html)
for fff in hh:
    url = "https://so.gushiwen.cn" + fff
    html = requests.get(url, headers=headers).text
    for i in re.findall("<meta name="description"content="[\d\D]+。" />",html):
      for c in re.findall("t="[\d\D]+"",i):
            e = c
            aa = jieba.lcut(e)
            for d in aa:
                if d != "," and d != "。" and d != ":" and d != "!" and d != "?" and d != "(" and d != ")" and d != "(" and d != ")" and d != ":" and d != " ":
                  f.append(d)
    print("第" + str(fg) + "首诗文分析成功")
    fg += 1

with open("data.json", mode='w', encoding="utf-8") as fd:
    fd.write(json.dumps(f, ensure_ascii=False))

isdkz 发表于 2022-2-14 19:23:51

因为你字符串里面的双引号跟外面的双引号冲突了,把字符串外面的双引号改成单引号就行

import requests
import re
import jieba
import json

fg = 1
f = []
urll = "https://so.gushiwen.cn/gushi/tangshi.aspx"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"}
html = requests.get(urll, headers=headers).text
hh = re.findall("/shiwenv_[\d\D]{17}", html)
for fff in hh:
    url = "https://so.gushiwen.cn" + fff
    html = requests.get(url, headers=headers).text
    for i in re.findall('<meta name="description"content="[\d\D]+。" />',html):                              #   改了这行
      for c in re.findall('t="[\d\D]+"',i):                                                                                    #改了这行
            e = c
            aa = jieba.lcut(e)
            for d in aa:
                if d != "," and d != "。" and d != ":" and d != "!" and d != "?" and d != "(" and d != ")" and d != "(" and d != ")" and d != ":" and d != " ":
                  f.append(d)
    print("第" + str(fg) + "首诗文分析成功")
    fg += 1

with open("data.json", mode='w', encoding="utf-8") as fd:
    fd.write(json.dumps(f, ensure_ascii=False))
页: [1]
查看完整版本: ':' expected错误