【发布时间】:2017-06-19 05:25:52
【问题描述】:
我是 Python 新手,我正在尝试编写一个网站爬虫来从 subreddits 获取链接,然后我可以稍后将其传递给另一个类,以便从 imagur 自动下载图像。
在这段代码 sn-p 中,我只是试图读取 subreddit 并从 hrefs 中抓取任何 imagur html,但我收到以下错误:
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()吗? -
请发布完整的错误消息,包括回溯。该错误不是由您的代码直接引起的,它来自您正在使用的库之一。
标签: python list web-scraping