BeautifulSoup网络抓取

时间:2016-03-30 20:10:43

标签: python beautifulsoup

我使用BeautifulSoup刮网站。当它返回链接时,它会以/watch/411012/1/test.html的形式出现 - 我该如何制作它以便返回http://website.co.uk/watch/411012/1/test.html

from bs4 import BeautifulSoup
import urllib2
import re

resp = urllib2.urlopen("http://website.co.uk") #add more sites
soup = BeautifulSoup(resp, from_encoding=resp.info().getparam('charset'))

for link in soup.find_all('a', href=re.compile('sports')): 
    print link['href']

1 个答案:

答案 0 :(得分:0)

使用urlparse.urljoin()

from urlparse import urljoin

base_url = "http://website.co.uk"
for link in soup.find_all('a', href=re.compile('sports')): 
    print urljoin(base_url, link['href'])