Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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';s";对象没有属性“;空页错误_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python-在Beautifulsoup';s";对象没有属性“;空页错误

Python-在Beautifulsoup';s";对象没有属性“;空页错误,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,为了提取我需要的文本,我可以在条件执行中使用Beautifulsoup的查找下一个兄弟姐妹来刮取大多数网页 if len(str(h4.find_next_sibling)) < 90: ... else: ... 如果len(str(h4.find_next_sibling))

为了提取我需要的文本,我可以在条件执行中使用Beautifulsoup的查找下一个兄弟姐妹来刮取大多数网页

if len(str(h4.find_next_sibling)) < 90:
    ...
else:
    ...
如果len(str(h4.find_next_sibling))<90:
...
其他:
...
但是,对于一个特定页面,该网页为空,因此Python会报告错误:

AttributeError:“非类型”对象没有“查找下一个兄弟”属性


由于空页似乎是由我计划刮取的页面列表中的错误产生的,我需要Python继续刮取而不在每个类似的实例上停止,一种可能是编写一个if条件,以便仅在页面中实际存在find_next_sibling时才运行上面的代码。有可能吗?任何想法都很感激

非常感谢评论者,使用try:except成功解决了此问题:

try: 
    if len(str(h4.find_next_sibling)) < 90:
        coverage = h4.find_next_sibling(text=True)
    else:
        if len(str(h4.find_next_sibling)[1]) < 90:
            coverage = h4.find_next_siblings(text=True)[2]
        else:
            coverage = h4.find_next_siblings(text=True)[1]
except:
    coverage = "Empty page" 
试试看:
如果len(str(h4.查找下一个兄弟姐妹))<90:
覆盖率=h4。查找下一个兄弟姐妹(text=True)
其他:
如果len(str(h4.查找下一个兄弟姐妹)[1])<90:
覆盖率=h4。查找下一个兄弟姐妹(text=True)[2]
其他:
覆盖率=h4。查找下一个兄弟姐妹(text=True)[1]
除:
coverage=“空白页”

怎么样
试试:除了:
?嘿,谢谢!我只是尝试将“Try:”放在if语句之前,将“except(RuntimeError、TypeError、NameError):coverage=“Empty page”放在最后一行之后,但仍然存在语法错误。。你能说得更具体点吗?@carl_pch谢谢大家。这对我来说第一次不起作用,因为我在try中以某种方式将T大写: