簡體   English   中英

Python BeautifulSoup - 從頁面獲取內部鏈接

[英]Python BeautifulSoup - Grab internal links from page

我有一個基本循環來查找我用urllib2.urlopen檢索的頁面上的鏈接,但是我只想跟蹤頁面上的內部鏈接。

任何想法如何使我的下面的循環只獲得在同一個域上的鏈接?

for tag in soupan.findAll('a', attrs={'href': re.compile("^http://")}): 
                webpage = urllib2.urlopen(tag['href']).read()
                print 'Deep crawl ----> ' +str(tag['href'])
                try:
                    code-to-look-for-some-data...

                except Exception, e:
                    print e
>>> import urllib
>>> print urllib.splithost.__doc__
splithost('//host[:port]/path') --> 'host[:port]', '/path'.

如果主機相同或主機為空(用於相對路徑),則url屬於同一主機。

for tag in soupan.findAll('a', attrs={'href': re.compile("^http://")}):

            href = tag['href']
            protocol, url = urllib.splittype(href) # 'http://www.xxx.de/3/4/5' => ('http', '//www.xxx.de/3/4/5')
            host, path =  urllib.splithost(url)    # '//www.xxx.de/3/4/5' => ('www.xxx.de', '/3/4/5')
            if host.lower() != theHostToCrawl and host != '':
                continue

            webpage = urllib2.urlopen(href).read()

            print 'Deep crawl ----> ' +str(tag['href'])
            try:
                code-to-look-for-some-data...

            except:
                import traceback
                traceback.print_exc()

因為你這樣做

'href': re.compile("^http://")

不會使用相對路徑。 就像

<a href="/folder/file.htm"></a>

也許根本不使用re?

針對您的爬蟲的一些建議:將機械化與BeautifulSoup結合使用,這將簡化您的任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM