English 中文(简体)
不应访问同一座右翼
原标题:Should not visit the same url

I am new to python, and i am developing a web crawler below is the program which get the links from given url, but the problem is i dont want it to visit the same url which is already visited. please help me.

import re
import urllib.request
import sqlite3
db = sqlite3.connect( test2.db )
db.row_factory = sqlite3.Row
db.execute( drop table if exists test )
db.execute( create table test(id INTEGER PRIMARY KEY,url text) )
#linksList = []
#module to vsit the given url and get the all links in that page
def get_links(urlparse):
        try:
            if urlparse.find( .msi ) ==-1: #check whether the url contains .msi extensions
                htmlSource = urllib.request.urlopen(urlparse).read().decode("iso-8859-1")  
                #parsing htmlSource and finding all anchor tags
                linksList = re.findall( <a href=(.*?)>.*?</a> ,htmlSource) #returns href and other attributes of a tag
                for link in linksList: 
                    start_quote = link.find( " ) # setting start point in the link 
                    end_quote = link.find( " , start_quote + 1) #setting end point in the link
                    url = link[start_quote + 1:end_quote] # get the string between start_quote and end_quote
                    def concate(url): #since few href may return only /contact or /about so concatenating its baseurl
                        if url.find( http:// ):
                            url = (urlparse) + url
                            return url
                        else:
                            return url
                    url_after_concate = concate(url)
#                    linksList.append(url_after_concate)
                    try:
                        if url_after_concate.find( .tar.bz ) == -1: # skipping links which containts link to some softwares or downloads page
                            db.execute( insert or ignore into test(url) values (?) , [url_after_concate])
                    except:
                        print("insertion failed")
            else:
                return True
        except:
            print("failed")
get_links( http://www.python.org )     
cursor = db.execute( select * from test )
for row in cursor: # retrieve the links stored in database
    print (row[ id ],row[ url ])
    urlparse = row[ url ]
#    print(linksList)
#    if urlparse in linksList == -1:
    try:
        get_links(urlparse) # again parse the link from database
    except:
        print ("url error")

请就如何解决这一问题提出建议。

最佳回答

你应该有一份访问页清单。 当你要求下一位法官时,你可以检查名单是否含有ur,如果是这样的话,则可以ski。 I m 不是on方案家,在这里是一些 pe形码

Create listOfVisitedUrls
...
Start Loop
Get nextUrl
If nextUrl IsNotIn listOfVisitedUrls Then
    Request nextUrl
    Add nextUrl to listOfVisitedUrls
End If
Loop
问题回答

You can use the following code:

import re
from urllib import urlopen

# Since few href may return only /contact or /about, concatenate to baseurl.
def concat(url, baseurl): 
    if url.find( http:// ):
        url = baseurl + url
        return url
    else:
        return url

def get_links(baseurl):
    resulting_urls = set()
    try:
        # Check whether the url contains .msi extensions.
        if baseurl.find( .msi ) == -1:
            # Parse htmlSource and find all anchor tags.
            htmlSource = urlopen(baseurl).read()
            htmlSource = htmlSource.decode("iso-8859-1")

            # Returns href and other attributes of a tag.
            linksList = re.findall( <a href=(.*?)>.*?</a> ,htmlSource)

            for link in linksList:
                # Setting start and end points in the link.
                start_quote = link.find( " )
                end_quote = link.find( " , start_quote + 1)

                # Get the string between start_quote and end_quote.
                url = link[start_quote + 1:end_quote]

                url_after_concat = concat(url, baseurl)
                resulting_urls.add(url_after_concat)

        else:
            return True

    except:
        print("failed")

    return resulting_urls

get_links( http://www.python.org )

http://www.python.org。

set([u http://www.python.org/download/ ,
 u http://docs.python.org/ ,
 u http://www.python.org#left-hand-navigation ,
 u http://wiki.python.org/moin/PyQt ,
 u http://wiki.python.org/moin/DatabaseProgramming/ ,
 u http://roundup.sourceforge.net/ ,
 u http://www.python.org/ftp/python/3.2.3/Python-3.2.3.tar.bz2 ,
 u http://www.python.org/about/website ,
 u http://www.python.org/about/quotes ,
 u http://www.python.org/community/jobs/ ,
 u http://www.python.org/psf/donations/ ,
 u http://www.python.org/about/help/ ,
 u http://wiki.python.org/moin/CgiScripts ,
 u http://www.zope.org/ ,
 u http://www.pygame.org/news.html ,
 u http://pypi.python.org/pypi ,
 u http://wiki.python.org/moin/Python2orPython3 ,
 u http://www.python.org/download/releases/2.7.3/ ,
 u http://www.python.org/ftp/python/3.2.3/python-3.2.3.msi ,
 u http://www.python.org/community/ ,
 u http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 ,
 u http://wiki.python.org/moin/WebProgramming ,
 u http://www.openbookproject.net/pybiblio/ ,
 u http://twistedmatrix.com/trac/ ,
 u http://wiki.python.org/moin/IntegratedDevelopmentEnvironments ,
 u http://www.pentangle.net/python/handbook/ ,
 u http://wiki.python.org/moin/TkInter ,
 u http://www.vrplumber.com/py3d.py ,
 u http://sourceforge.net/projects/mysql-python ,
 u http://wiki.python.org/moin/GuiProgramming ,
 u http://www.python.org/about/ ,
 u http://www.edgewall.com/trac/ ,
 u http://osl.iu.edu/~lums/swc/ ,
 u http://www.python.org/community/merchandise/ ,
 u"http://www.python.org /psf/",
 u http://wiki.python.org/moin/WxPython ,
 u http://docs.python.org/3.2/ ,
 u http://www.python.org#content-body ,
 u http://www.python.org/getit/ ,
 u http://www.python.org/news/ ,
 u http://www.python.org/search ,
 u http://www.python.org/community/sigs/current/edu-sig ,
 u http://www.python.org/about/legal ,
 u http://www.timparkin.co.uk/ ,
 u http://www.python.org/about/apps ,
 u http://www.turbogears.org/ ,
 u http://www.egenix.com/files/python/mxODBC.html ,
 u http://docs.python.org/devguide/ ,
 u http://docs.python.org/howto/sockets.html ,
 u http://www.djangoproject.com/ ,
 u http://buildbot.net/trac ,
 u http://www.python.org/psf/ ,
 u http://www.python.org/doc/ ,
 u http://wiki.python.org/moin/Languages ,
 u http://www.xs4all.com/ ,
 u http://www.python.org/ ,
 u http://wiki.python.org/moin/NumericAndScientific ,
 u http://www.python.org/channews.rdf ,
 u http://www.alobbs.com/pykyra ,
 u http://wiki.python.org/moin/PythonXml ,
 u http://wiki.python.org/moin/PyGtk ,
 u http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi ,
 u http://www.python.org/download/releases/3.2.3/ ,
 u http://www.python.org/3kpoll ])

希望会有所帮助。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签