批处理之家's Archiver

bingxing8000 发表于 2018-4-13 17:31

【已解决】报错: maximum recursion depth exceeded while calling a Python object

[i=s] 本帖最后由 bingxing8000 于 2018-4-16 15:12 编辑 [/i]

[code]
# pip install requests        # 安装爬虫模块,用来抓取网页的html源代码
# pip install beautifulsoup4  # 安装html代码解析模块
# pip install prettytable #安装PrettyTable模块,可以将输出内容如表格方式整齐地输出

import requests    # 导入爬虫模块
from bs4 import BeautifulSoup  # 导入html代码解析模块
from prettytable import PrettyTable # 导入整齐输出模块

url = "http://www.weather.com.cn/weather/101210101.shtml"
get_html = requests.get(url)    # 获取html源代码
soure = BeautifulSoup(get_html.content,"html.parser")
body = soure.find("body")        # 找到body标签
div = body.find("div",id = "7d") # 找到div开头,并且属性有"id="7d""的标签
ul = div.find("ul")    # 从 div开头并且属性有"id="7d""的标签 中找到ul标签
li = ul.find_all("li") # 找到所有的li标签

list=[]
for shuju in li:  # 遍历标签
    dic={}
    riqi = shuju.find("h1").string  # 找到hl标签并提取日期
    dic["date"] = riqi              # 把获取到的添加到字典
    tianqi= shuju.find_all("p")     # 找到所有的p标签
    dic["tq"] = tianqi[0].string    #
    wendu = tianqi[1].find("span")
    dic["maxwd"] = wendu.string + "℃"
    wendu2= tianqi[1].find("i")
    dic["minwd"] = wendu2.string
    fengxiang = tianqi[2].find("span")["title"]
    dic["fx"] = fengxiang
    fengli = tianqi[2].find("i")
    dic["fl"] = fengli.string
    list.append(dic)

table = PrettyTable(["日期", "天气","最高温度","最低温度","风向","风力"])
#for i in range(0,7):
#   a = list[i]["date"],list[i]["tq"], list[i]["maxwd"], list[i]["minwd"], list[i]["fx"], list[i]["fl"]
#   print(a)
for i in range(0,7):
    table.add_row([list[i]["date"],list[i]["tq"], list[i]["maxwd"], list[i]["minwd"], list[i]["fx"], list[i]["fl"]])
print(table)
[/code]请问:为什么会报ecursionError: maximum recursion depth exceeded while calling a Python object 错误呢?注释部分运行就可以的。

codegay 发表于 2018-4-13 17:49

requests只是一个http的客户端,不是爬虫模块。

bingxing8000 发表于 2018-4-16 09:25

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=207696&ptid=47833]2#[/url] [i]codegay[/i] [/b]

你好,谢谢你的指导。但我的问题是代码中的这一段[code]
table = PrettyTable(["日期", "天气","最高温度","最低温度","风向","风力"])
#for i in range(0,7):
#   a = list[i]["date"],list[i]["tq"], list[i]["maxwd"], list[i]["minwd"], list[i]["fx"], list[i]["fl"]
#   print(a)
for i in range(0,7):
    table.add_row([list[i]["date"],list[i]["tq"], list[i]["maxwd"], list[i]["minwd"], list[i]["fx"], list[i]["fl"]])
print(table)
[/code]现在这段报错,提示是RecursionError: maximum recursion depth exceeded while calling a Python object,网上说是递归层太多,但是我这没怎么递归呀,只是把列表下面字典的值取出来而已。
把没有注释的部分注释掉,再把注释的部分取消注释可以正常运行。
麻烦帮我看一下是什么问题,谢谢。

cfwyy77_bat 发表于 2018-4-16 12:42

python 区人气好低的~  
问题应该在prettytable 模块,这个模块我也不熟,没用过,不过我看了一下它 都5年没更新过了。
我的环境是python3.6。
把prettytable.py 第926行
改成:[code]rows = self._rows[options["start"]:options["end"]][/code]保存。他原来是deepcopy,我感觉没必要啊,我也不知道如何解释好- -
然后应该就可以跑了。

我还把你后面那段稍改了一下:[code]tabl = PrettyTable(["日期", "天气","最高温度","最低温度","风向","风力"])
templst=[list(dic.values()) for dic in lst]
for i in range(7):
    tabl.add_row(templst[i])
print(tabl)[/code]其中你的变量名list  我改成了lst,因为list  是内建的列表类,用来作自定义的变量名不太好,虽然也能跑。

cfwyy77_bat 发表于 2018-4-16 14:51

又改了一下,可以不用字典。[code]import requests    # 导入爬虫模块
from bs4 import BeautifulSoup  # 导入html代码解析模块
from prettytable import PrettyTable # 导入整齐输出模块

url = "http://www.weather.com.cn/weather/101210101.shtml"
get_html = requests.get(url)    # 获取html源代码
soure = BeautifulSoup(get_html.content,"html.parser")
body = soure.find("body")        # 找到body标签
div = body.find("div",id = "7d") # 找到div开头,并且属性有"id="7d""的标签
ul = div.find("ul")    # 从 div开头并且属性有"id="7d""的标签 中找到ul标签
li = ul.find_all("li") # 找到所有的li标签

lst = [[i.find("h1").string,
    i.find_all("p")[0].string,
    i.find_all("p")[1].find("span").string+"℃",
    i.find_all("p")[1].find("i").string,
    i.find_all("p")[2].find("span")["title"],
    i.find_all("p")[2].find("i").string] for i in li]

table = PrettyTable(["日期", "天气","最高温度","最低温度","风向","风力"])
for i in range(7):
    table.add_row(lst[i])
print(table)[/code]

bingxing8000 发表于 2018-4-16 15:10

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=207745&ptid=47833]4#[/url] [i]cfwyy77_bat[/i] [/b]
非常感谢,确实按您所说的把第926行,改成您那样的就可以运行了。
另外感谢您在解决问题的同时对其他方面的指导,谢谢。

bingxing8000 发表于 2018-4-16 15:16

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=207748&ptid=47833]5#[/url] [i]cfwyy77_bat[/i] [/b]

再次感谢您

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.