Python BeautifulSoup从pag获取内部链接

2024-09-30 10:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基本的循环来查找我用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

Tags: inrefor链接tag页面urllib2attrs
2条回答

给你的爬虫一些建议:将mechanize与BeautifulSoup结合使用,这将大大简化你的任务。在

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

如果主机相同或主机为空(表示相对路径),则url属于同一主机。在

^{pr2}$

因为你这么做

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

不会使用相对路径。 他们就像

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

也许根本不用re?在

相关问题 更多 >

    热门问题