【问题标题】:Python Web Scraping using BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text'Python Web Scraping using BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text'
【发布时间】:2019-06-04 19:25:11
【问题描述】:

我不确定为什么在使用 BeautifulSoup 在 WebScraping 上运行 Python 2 中的代码时收到错误消息 AttributeError: 'NoneType' object has no attribute 'text'?

错误信息:

AttributeError Traceback(最近调用 最后)

在 ()

----> 1 个名称 = name_box.text.strip()

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

以下是我的代码:

from bs4 import BeautifulSoup

import requests

import csv

source = requests.get('http://coreyms.com').text

soup = BeautifulSoup(source, 'lxml')

name_box = soup.find('h1', attrs = {'class': 'name'})

name = name_box.text.strip()

你能帮忙吗?谢谢!

【问题讨论】:

    标签: python beautifulsoup python-requests


    【解决方案1】:

    您收到该响应是因为页面中的“名称”类中没有 h1 标记。我能找到的唯一 h1 标签有一个“site-title”类。

    from bs4 import BeautifulSoup
    import requests
    import csv
    source = requests.get('http://coreyms.com').text
    soup = BeautifulSoup(source, 'lxml')
    name_box = soup.find('h1', attrs = {'class': 'site-title'})
    name = name_box.text.strip()
    print(name)
    

    输出

    CoreyMS
    

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2022-12-15
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      相关资源
      最近更新 更多