问题
I have made a web crawler which gets all links till the 1st level of page and from them it gets all link and text plus imagelinks and alt. here is whole code:
import urllib
import re
import time
from threading import Thread
import MySQLdb
import mechanize
import readability
from bs4 import BeautifulSoup
from readability.readability import Document
import urlparse
url = ["http://sparkbrowser.com"]
i=0
while i<len(url):
counterArray = [0]
levelLinks = []
linkText = ["homepage"]
levelLinks = []
def scraper(root,steps):
urls = [root]
visited = [root]
counter = 0
while counter < steps:
step_url = scrapeStep(urls)
urls = []
for u in step_url:
if u not in visited:
urls.append(u)
visited.append(u)
counterArray.append(counter +1)
counter +=1
levelLinks.append(visited)
return visited
def scrapeStep(root):
result_urls = []
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_equiv(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
for url in root:
try:
br.open(url)
for link in br.links():
newurl = urlparse.urljoin(link.base_url, link.url)
result_urls.append(newurl)
#levelLinks.append(newurl)
except:
print "error"
return result_urls
scraperOut = scraper(url[i],1)
for sl,ca in zip(scraperOut,counterArray):
print "\n\n",sl," Level - ",ca,"\n"
#Mechanize
br = mechanize.Browser()
page = br.open(sl)
br.set_handle_robots(False)
br.set_handle_equiv(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
#BeautifulSoup
htmlcontent = page.read()
soup = BeautifulSoup(htmlcontent)
for linkins in br.links(text_regex=re.compile('^((?!IMG).)*$')):
newesturl = urlparse.urljoin(linkins.base_url, linkins.url)
linkTxt = linkins.text
print newesturl,linkTxt
for linkwimg in soup.find_all('a', attrs={'href': re.compile("^http://")}):
imgSource = linkwimg.find('img')
if linkwimg.find('img',alt=True):
imgLink = linkwimg['href']
#imageLinks.append(imgLink)
imgAlt = linkwimg.img['alt']
#imageAlt.append(imgAlt)
print imgLink,imgAlt
elif linkwimg.find('img',alt=False):
imgLink = linkwimg['href']
#imageLinks.append(imgLink)
imgAlt = ['No Alt']
#imageAlt.append(imgAlt)
print imgLink,imgAlt
i+=1
Everything is working great until my crawler reaches one of facebook links
which he can't read, but he gives me error
httperror_seek_wrapper: HTTP Error 403: request disallowed by robots.txt
for the line 68 which is: page = br.open(sl)
And I don't now why because as you can see, I've setted up mechanize set_handle_robots
and add_headers
options.
I don't know why is that but I noticed that I'm getting that error for facebook
links, in this case facebook.com/sparkbrowser
and google to.
Any help or advice is welcome.
cheers
回答1:
Ok, so the same problem appeared in this question:
Why is mechanize throwing a HTTP 403 error?
By sending all the request headers a normal browser would send, and accepting / sending back the cookies the server sends should resolve the issue.
来源:https://stackoverflow.com/questions/18096885/python-mechanize-request-disallowed-by-robots-txt-even-after-set-handle-robot