我是Python新手,我正在嘗試編寫一個網站刮板來獲取來自subreddits的鏈接,然後我可以稍後將其傳遞給另一個類以自動下載來自imagur的圖像。Python列表對象沒有任何屬性錯誤
在此代碼段,我只是想閱讀版(Subreddit)和HREF中刮任何imagur 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)
發佈完整的回溯或提及行號。 –
你在urlopen上使用'.read()'嗎? –
請發佈完整的錯誤消息,包括回溯。該錯誤不是由您的代碼直接導致的,而是來自您正在使用的某個庫。 – kindall