Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup-嵌套的Div和类问题_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python BeautifulSoup-嵌套的Div和类问题

Python BeautifulSoup-嵌套的Div和类问题,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,对python和webscraping来说有点新-希望这只是我在语法上遗漏的东西。我试着通读了一个类似的问题,但是我正在爬行的源代码中的元素可能有点不同。试图从本质上刮取3个元素: “工作室” “386平方英尺|每月分期付款起价1030美元”, “1030”来自以下内容: <div class="fp-info has-description"> <div class="row">

对python和webscraping来说有点新-希望这只是我在语法上遗漏的东西。我试着通读了一个类似的问题,但是我正在爬行的源代码中的元素可能有点不同。试图从本质上刮取3个元素:

“工作室” “386平方英尺|每月分期付款起价1030美元”, “1030”来自以下内容:

<div class="fp-info has-description">
                        <div class="row">
                            <div class="fp-title col-lg-7">
                                <h4 class="title">Studio</h4>
                            </div>
                            <div class="fp-avil col-lg-5">
                                <p class="small fp-avail-sum"><i class="fa fa-check-circle"></i> Available</p>
                            </div>
                        </div>
                            <p>386 SQ. FT. | Starting at $1030 Per Monthly Installment</p>
                        <div class="clear"></div>
                            <div class="fp-description">

                                <div class="icon-box effect small clean">
                                    <div class="icon"><a href="#"><i class="fa fa-info-circle"></i></a></div>

                                    <p class="text-uppercase"><small>Smart Housing Only.</small></p>
                                </div>
                            </div>
编辑-我得到的完整回溯错误为: 对于scrap中的计划(key,keyLinkMap[key]):文件“C:\Users\Michael\Desktop\Python Tests\edrsrape.py”,第40行,在scrap中 对于汤中的计划。find_all('div',{'class':'fp-info has description'}): UnboundLocalError:赋值前引用了局部变量“soup”


有什么想法吗?我有一个类似的刮刀,我是遗赠的,它做了一些非常相似的事情,但它工作得很好。任何帮助都将不胜感激,谢谢

问题在于这些线路:

try:
    soup = getWebData(val)
except:
    plans
getWebData(val)
抛出异常时,变量
soup
不会被赋值。因此,当您在这里使用它时,
soup.find_all(…)
,您使用的是一个尚未定义的变量
soup
。因此,消息“赋值前引用了局部变量'soup'

你可以这样做来纠正这个问题

try:
    soup = getWebData(val)
except:
    return plans

在这里,如果抛出异常,程序将无法到达
soup.find_all(…)
语句。

能否解释异常处理的基本原理?请在问题中包含完整的回溯错误消息。调试会更容易。这里的问题很可能是您在
soup=getWebData(val)
行中遇到错误。由于这个原因,python移动到
,除了
之外,它只是
计划
。因此,
soup
从未定义过。因此,在计划
的起始行中,您有一个变量soup,它在被分配之前被引用。我还将回应其他评论,关于完整的回溯错误是有用的,这意味着我们不需要猜测,以及异常处理的基本原理。谢谢!唯一的问题是-当以1030美元的价格提供studio的数据时,它不应该抛出异常-它应该返回该数据。我不明白您现在的问题是什么,但是
UnboundLocalError
问题应该通过做此更改来解决。另外,我不能说它为什么会抛出异常,因为我不知道你在
getWebData
函数中写了什么。抱歉!def getWebData(link):返回BeautifulSoup(requests.get(link.text,'html.parser')该函数唯一可能出错的地方是该链接不是有效的url。通过在
except
块中打印来检查错误是什么。
try:
    soup = getWebData(val)
except:
    return plans