【问题标题】:Why am I getting a "None" in my Python code? [closed]为什么我的 Python 代码中显示“无”? [关闭]
【发布时间】:2016-03-18 21:28:42
【问题描述】:

我正在尝试循环浏览六个维基百科页面以获取每首链接歌曲的列表。当我在终端中运行它时,它给了我这个错误:

Traceback (most recent call last):
  File "scrapeproject.py", line 31, in <module>
    print (getTableLinks(my_url))
  File "scrapeproject.py", line 20, in getTableLinks
    html = urlopen(my_url)
  File "/Users/adriana/Software/Python-3.5.1/mybuild/lib/python3.5/urllib/request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/adriana/Software/Python-3.5.1/mybuild/lib/python3.5/urllib/request.py", line 456, in open
    req.timeout = timeout
AttributeError: 'NoneType' object has no attribute 'timeout'

我认为这是因为当我打印歌曲列表时,None 不断出现。有人有什么建议吗?

代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import sys
import http.client

main = "https://en.wikipedia.org/wiki/Billboard_Year-End_Hot_100_singles_of_"
year = 2009

def createUrl(main, year):
    for i in range(0, 6): # increment years so i can get each link
        year += 1
        print ("\n\n", year, "\n\n")
        fullUrl = main + str(year)
        return fullUrl


my_url = createUrl(main, year) # this is how i make createUrl a variable to be used in other functions

def getTableLinks(my_url): # there is a random none appearing in my code

    # i think the problem is between here...

    html = urlopen(my_url)
    bsObj = BeautifulSoup(html.read(), "html.parser")
    tabledata = bsObj.find("table", {"class":"wikitable"}).find_all("tr")

    # ...and here

    for table in tabledata:
        try:
            links = table.find("a")
            if 'href' in links.attrs:
                print (links.attrs['href'])
        except:
            pass

print (getTableLinks(my_url))

【问题讨论】:

    标签: python loops for-loop


    【解决方案1】:

    你没有从 createUrl 返回任何东西,所以 None 被返回

    如果您想创建一批六个 url,然后解析数据/进行网络抓取。我建议将它们附加到列表或将每个 url 映射到函数以进行程序解析,然后要么这样做或返回列表并遍历它以进行解析。

    【讨论】:

    • 因此,如果我从 print (fullUrl) 更改为返回 fullUrl,它会给我:2010 /wiki/Tik_Tok /wiki/Need_You_Now_(Lady_Antebellum_song) /wiki/Hey,_Soul_Sister /wiki/California_Gurls ... /wiki/Teach_Me_How_to_Dougie /wiki/Try_Sleeping_with_a_Broken_Heart /wiki/Lover,_Lover None 这正是我想要的,但我希望它在 2015 年之前做到这一点,而不是停在那里.我认为 None 意味着由于某种原因它无法进入 2011 年?
    【解决方案2】:

    问题不在您突出显示的区域。问题出在构建 fullUrl 的循环中。完全摆脱它,因为您不需要函数来构建链接。

    然后在你的函数定义下,尝试:

    for n in range(2008,2015):
        print(getTableLinks(main + str(n)))
    

    根据您的需要更改年份。

    老实说,为了将来使用,更好的方法是使用错误处理。这将允许您运行该函数,直到没有剩余年份(抛出异常)并且循环将退出。这使您不必检查有多少年,您所要做的就是调整起始年份。要正确执行此操作,您需要查找错误处理并专门处理通过尝试无效年份返回的错误,并执行类似 except AttributeError: 或使用代码示例的任何错误以下。

    for n in range(2008,2015):
        try:
            print(getTableLinks(main + str(n)))
        except:
            break
    

    【讨论】:

    • 非常感谢!我仍然得到一个奇怪的 None 但它最终循环通过我的代码并获取我需要的所有链接。
    • 好。随意投票。
    • 很想...但我太新了,没有资格大声笑
    • @Chris 郑重声明,“请随意投票”是一句粗鲁的话。以适当的谨慎和谦逊的态度要求接受可能更合适。但首先你应该等待OP自己选择。只有当您怀疑他们(例如您自己)对网站的运作缺乏了解时,您才应该尝试指导他们接受和支持的工作原理。
    • 然后继续练习;) 语气绝对应该是@​​987654321@,并且您应该等待一段时间(至少一天!),然后再向 OP 提出任何问题。 (这就是全部,我现在就让你一个人呆着,只是想清楚我的意思。)
    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2013-12-03
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多