我使用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']
答案 0 :(得分:0)
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'])