简体   繁体   English

AttributeError:'NoneType'对象没有属性'strip'

[英]AttributeError: 'NoneType' object has no attribute 'strip'

I've been trying to learn Python (currently requests and beautifulsoup4) and I found a tutorial online 我一直在努力学习Python(目前的请求和beautifulsoup4),我在网上找到了一个教程

The issue is I keep getting the below error and cannot figure it out at all... 问题是我一直得到以下错误,根本无法搞清楚...

Any help would be appreciated! 任何帮助,将不胜感激!

Traceback (most recent call last): File "C:\\Users\\BillyBob\\Desktop\\Web Scrap.py", line 14, in title = a.string.strip() AttributeError: 'NoneType' object has no attribute 'strip' 回溯(最近一次调用最后一次):文件“C:\\ Users \\ BillyBob \\ Desktop \\ Web Scrap.py”,第14行,标题= a.string.strip()AttributeError:'NoneType'对象没有属性'strip'

Here is my code in case I made a mistake; 这是我的代码,以防我犯了错误;

import requests
from bs4 import BeautifulSoup

result = requests.get("http://www.oreilly.com/")

c = result.content

soup = BeautifulSoup(c, "html.parser")
samples = soup.find_all("a")
samples[0]

data = {}
for a in samples:
    title = a.string.strip()
    data[title] = a.attrs['href']

From BS4 documentation : BS4文档

If a tag contains more than one thing, then it's not clear what .string should refer to, so .string is defined to be None 如果一个标签包含多个东西,那么不清楚.string应该引用什么,所以.string被定义为None

I believe you can use .text to get what you want: 我相信你可以使用.text来获得你想要的东西:

title = a.text.strip()

The first member of samples does not have a string attribute, and as a result, a.string doesn't return anything, so you're calling the strip() method on something that doesn't exist. samples的第一个成员没有字符串属性,因此, a.string不返回任何内容,因此您在不存在的内容上调用strip()方法。

However, then you have another problem; 但是,那你还有另一个问题; it is not necessarily true that a has the href attribute. a具有href属性不一定是真的。 Instead, you should check explicitly for both, or else you will get errors (which is the problem with Yevhen's answer, which is otherwise correct). 相反,你应该明确检查两者,否则你会得到错误(这是Yevhen的答案的问题,否则是正确的)。

One potential fix to your problem is to write: 解决问题的一个可能方法是:

for a in samples:
    if not a.string is None:
        title = a.string.strip()
        if 'href' in a.attrs.keys():
            data[title] = a.attrs['href']

This way, you check explicitly for each parameter before calling the associated method. 这样,在调用关联方法之前,会显式检查每个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'NoneType' 对象没有属性 'strip' jupyter notebook - AttributeError: 'NoneType' object has no attribute 'strip' jupyter notebook 我不断收到错误:AttributeError: 'NoneType' object has no attribute 'strip' - I keep getting the error: AttributeError: 'NoneType' object has no attribute 'strip' 如何修复 AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? - how to fix AttributeError: 'NoneType' object has no attribute 'strip' in this specific code? AttributeError: 'NoneType' object has no attribute 'strip' 如何解决? - AttributeError: 'NoneType' object has no attribute 'strip' how to solve? AttributeError:“ NoneType”对象在编辑CSV时没有属性“ strip” .. - AttributeError: 'NoneType' object has no attribute 'strip'..while editing CSV Python discord 机器人 -- AttributeError: 'NoneType' object 没有属性 'strip' - Python discord bot -- AttributeError: 'NoneType' object has no attribute 'strip' AttributeError:“ NoneType”对象在Python WebCrawler中没有属性“ strip” - AttributeError: 'NoneType' object has no attribute 'strip' with Python WebCrawler 我该如何解决这个问题:AttributeError: 'NoneType' object has no attribute 'strip - How can i fix this :AttributeError: 'NoneType' object has no attribute 'strip 获取 AttributeError: 'NoneType' object 在读取 XML 时没有属性 'strip' - Getting AttributeError: 'NoneType' object has no attribute 'strip' while reading XML AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM