【问题标题】:mechanize, urllib, beautifulsoup & relative pathsmechanize、urllib、beautifulsoup 和相关路径
【发布时间】:2012-06-12 09:55:54
【问题描述】:
mechanize、urllib 或 beautifulsoup 是否有任何内置方法来处理混合使用绝对 URL 和相对 URL 的网站抓取?
一个解决方案是有很多例外
'http://' + 'www.stackoverflow.com'
'http://www.stackoverflow.com' + '/questions/ask'
还有更好的选择吗?
【问题讨论】:
标签:
python
web-crawler
beautifulsoup
mechanize
urllib2
【解决方案1】:
为了记录,这是我的解决方案:)
domain = re.search('(http:\/\/.*\.\D+?|https:\/\/.*\.\D+?)\/',url.strip()).group(1)
if re.search('mailto',url.strip()) != None:
pass
elif re.search('(http:\/\/.*\.\D+?|https:\/\/.*\.\D+?)\/',url.strip()) != None:
u = url.strip().encode('utf8')
elif re.search('^/',url.strip()) != None:
u = domain+url.strip().encode('utf8')
else:
u = domain+'/'+url.strip().encode('utf8')