如何刮取页面上的网站链接?

时间:2015-09-21 23:19:05

标签: python web-scraping

我尝试使用{“a”,{“target”:“_ blank”})来获取此商家信息中的网址,但这不起作用。

在这种情况下刮掉链接的正确方法是什么?我应该使用{“target”:“某事”}还是{“href”:“某事”}?

import requests
from bs4 import BeautifulSoup

url = "http://www.gym-directory.com/listing/bulldog-gym/"
r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')

print soup.find("span",{"class":"wlt_shortcode_TITLE"}).text 
print soup.find("span",{"class":"wlt_shortcode_map_location"}).text 
print soup.find("span",{"class":"wlt_shortcode_phoneNum"}).text
print soup.find("span",{"class":"wlt_shortcode_EMAIL"}).text
print soup.find("a",{"target":"_blank"}).text

1 个答案:

答案 0 :(得分:3)

您需要href

soup.find("a", {"target":"_blank"})["href"]