我嘗試使用Python2.7抓取整個網站:如何使用Python 2.7遵守robots.txt?
- 我使用robotparser
- 我打開每一個環節「a」到網站,並
- 我將它們添加到分析的robots.txt文件要檢索的頁面列表 關鍵是: 我試圖避免Robots.txt文件中的所有路徑,但它們仍然在要爬網的頁面列表中。
如何從我的爬網列表中刪除Robot.txt路徑?
我cound't找到任何幫助,通過計算器呢。
我的代碼波紋管:
import robotparser
import urlparse
import urllib
import urllib2
from BeautifulSoup import *
AGENT_NAME = 'PYMOTW'
URL_BASE = 'website'
urls = [URL_BASE]
visited = [URL_BASE] # Create a copy
parser = robotparser.RobotFileParser()
parser.set_url(urlparse.urljoin(URL_BASE, 'robot.txt'))
parser.read()
PATHS = [
'/..../',
]
for path in PATHS:
print '%6s : %s' % (parser.can_fetch(AGENT_NAME, path), path)
url = urlparse.urljoin(URL_BASE, path)
print '%6s : %s' % (parser.can_fetch(AGENT_NAME, url), url)
robot = [url]
while (len(urls) > 0 and robot != True):
html = urllib.urlopen(urls[0]).read()
soup = BeautifulSoup(html) # Parse All HTML using BeautifulSoup
urls.pop(0)
# Retrieve all of Tags as a list
for tags in soup.findAll('a', href = True):
tags['href'] = urlparse.urljoin(URL_BASE, tags['href'])
if URL_BASE in tags['href'] and tags['href'] not in visited:
urls.append(tags['href'])
visited.append(tags['href'])
c = len(visited)
print visited
print 'page visited', c
歡迎堆棧溢出!我編輯了您的帖子,以刪除僅適用於在Web瀏覽器中運行的HTML/JavaScript的代碼段功能。除了刪除Python 3標籤之外,我還修復了拼寫和添加格式以提高可讀性。像這樣改進你的問題會增加你閱讀你的問題並獲得很好答案的機會。 –
謝謝@AnthonyGeoghegan – CDS
Hi @ J.F.Sebastian。返回值是True值的列表。 – CDS