1
我正在研究从特定页面提取url的bot。我已经提取了所有链接并将它们放入列表中,现在我似乎无法从列表中获取实际的URL(导致其他网站以http或https开头),并将它们附加到另一个列表中,或者删除那些不包含“从http开始。在此先感谢从列表python中提取特定元素2.7
import urllib2
import requests
from bs4 import BeautifulSoup
def main():
#get all the links from bing about cancer
site = "http://www.bing.com/search?q=cancer&qs=n&form=QBLH&pq=cancer&sc=8-4&sp=-1&sk=&cvid=E56491F36028416EB41694212B7C33F2"
urls =[]
true_links = []
r = requests.get(site)
html_content = r.content
soup = BeautifulSoup(html_content, 'html.parser')
links = soup.find_all("a")
for link in links:
link = link.get("href")
urls.append(str(link))
#urls.append(link.get("href"))
#print map(str, urls)
#REMOVE GARBAGE LINKS
print len(urls)
print urls
main()
你能详细说明这个问题吗?如果我在写代码的时候运行你的代码,那么'urls'就会填充一个URL列表,其中许多指向除bing之外的其他站点(例如'...'http://www.coursera.org/course/ clinicaltrials','http://www.coursera.org/course/clinicaltrials','http://www.khanacademy.org/science/health-and-medicine/respiratory-system-diseases/lung-cancer/v/肺癌并发症',...')你能解释你得到的结果与你想要的结果有什么不同吗? – larsks
我想链接到一个实际的网站,而不是一个脚本或样式表(例如我希望http://www.webmd.com/cancer/default.htm不是/script.js或/styles.css – MFK34