【发布时间】:2014-06-21 13:43:31
【问题描述】:
我正在学习构建网络爬虫,目前正致力于从网站获取所有网址。我一直在玩,没有与以前相同的代码,但我已经能够获得所有链接,但我的问题是递归我需要一遍又一遍地做同样的事情,但我认为我的问题是它所做的递归适合我编写的代码。我的代码如下
#!/usr/bin/python
import urllib2
import urlparse
from BeautifulSoup import BeautifulSoup
def getAllUrl(url):
page = urllib2.urlopen( url ).read()
urlList = []
try:
soup = BeautifulSoup(page)
soup.prettify()
for anchor in soup.findAll('a', href=True):
if not 'http://' in anchor['href']:
if urlparse.urljoin('http://bobthemac.com', anchor['href']) not in urlList:
urlList.append(urlparse.urljoin('http://bobthemac.com', anchor['href']))
else:
if anchor['href'] not in urlList:
urlList.append(anchor['href'])
length = len(urlList)
for url in urlList:
getAllUrl(url)
return urlList
except urllib2.HTTPError, e:
print e
if __name__ == "__main__":
urls = getAllUrl('http://bobthemac.com')
for x in urls:
print x
我想要实现的是获取具有当前设置的站点的所有 url,程序运行直到内存不足,我想要的只是从站点获取 url。有没有人知道如何做到这一点,认为我的想法是正确的,只需要对代码进行一些小的改动。
编辑
对于你们这些感兴趣的人,下面是我的工作代码,它可以获取网站的所有内容,有人可能会觉得它有用。这不是最好的代码,确实需要一些工作,但通过一些工作可能会非常好。
#!/usr/bin/python
import urllib2
import urlparse
from BeautifulSoup import BeautifulSoup
def getAllUrl(url):
urlList = []
try:
page = urllib2.urlopen( url ).read()
soup = BeautifulSoup(page)
soup.prettify()
for anchor in soup.findAll('a', href=True):
if not 'http://' in anchor['href']:
if urlparse.urljoin('http://bobthemac.com', anchor['href']) not in urlList:
urlList.append(urlparse.urljoin('http://bobthemac.com', anchor['href']))
else:
if anchor['href'] not in urlList:
urlList.append(anchor['href'])
return urlList
except urllib2.HTTPError, e:
urlList.append( e )
if __name__ == "__main__":
urls = getAllUrl('http://bobthemac.com')
fullList = []
for x in urls:
listUrls = list
listUrls = getAllUrl(x)
try:
for i in listUrls:
if not i in fullList:
fullList.append(i)
except TypeError, e:
print 'Woops wrong content passed'
for i in fullList:
print i
【问题讨论】:
-
看起来你的函数没有返回任何东西。
-
是的,这是一项正在进行中的工作,'print urlList' 是返回的地方,我只是想尝试一下。编辑以显示退货的情况。
-
讨厌人们无缘无故地给负面标记
-
你创建了一个递归并且永远不会破坏它,我认为这让你的程序永远不会结束 util 内存不足。
-
我知道这一点我在我的帖子中提到了这一点,我想看看我使用的方法是否正确以及如何破解它。
标签: python beautifulsoup urllib2 web-crawler