Python列表对象没有属性错误

时间:2017-06-19 05:25:52

标签: python list web-scraping

我是Python的新手,我正在尝试编写一个网站刮刀来获取subreddits的链接,然后我可以将其传递给另一个类,以便从imagur自动下载图像。

在这段代码中,我只是想读取subreddit并从hrefs中删除任何想象htmls,但是我收到以下错误:

AttributeError: 'list' object has no attribute 'timeout'

知道为什么会发生这种情况?这是代码:

from bs4 import BeautifulSoup
from urllib2 import urlopen
import sys
from urlparse import urljoin

def get_category_links(base_url):
    url = base_url
    html = urlopen(url)
    soup = BeautifulSoup(html)
    posts = soup('a',{'class':'title may-blank loggedin outbound'})
    #get the links with the class "title may-blank "
    #which is how reddit defines posts
    for post in posts:
        print post.contents[0]
        #print the post's title

        if post['href'][:4] =='http':
            print post['href']
        else:
            print urljoin(url,post['href'])
        #print the url.  
        #if the url is a relative url,
        #print the absolute url.   


get_category_links(sys.argv)

1 个答案:

答案 0 :(得分:4)

看看你如何调用这个函数:

get_category_links(sys.argv)

sys.argv这里是脚本参数列表,其中第一项是脚本名称本身。这意味着您的base_url参数值是导致urlopen失败的列表:

>>> from urllib2 import urlopen
>>> urlopen(["I am", "a list"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
           │           │    │     └ <object object at 0x105e2c120>
           │           │    └ None
           │           └ ['I am', 'a list']
           └ <urllib2.OpenerDirector instance at 0x105edc638>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in open
    req.timeout = timeout
    │             └ <object object at 0x105e2c120>
    └ ['I am', 'a list']
AttributeError: 'list' object has no attribute 'timeout'

您打算从sys.argv获取第二个参数并将其传递给get_category_links

get_category_links(sys.argv[1])

虽然有趣但是,在这种情况下,如何理解错误是多么神秘和难以理解。这来自"url opener" works in Python 2.7的方式。如果url值(第一个参数)不是字符串,则假定它是Request实例并尝试在其上设置timeout值:

def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
    # accept a URL or a Request object
    if isinstance(fullurl, basestring):
        req = Request(fullurl, data)
    else:
        req = fullurl
        if data is not None:
            req.add_data(data)

    req.timeout = timeout  # <-- FAILS HERE

请注意behavior have not actually changed in the latest stable 3.6 as well