我想知道哪种编程语言提供了大量的库来编写网络爬虫?比如抓取网页数据。例如,我想从 weather.yahoo.com 网站获取天气。
另外,对于 AI 桌面机器人,答案是否相同?
回答:
以下是如何在 Python 中执行此操作:
from urllib2 import urlopenfrom BeautifulSoup import BeautifulSoupsoup=BeautifulSoup(urlopen("http://weather.yahoo.com/").read())for x in soup.find(attrs={"id":"myLocContainer"}).findAll("li"): print x.a["title"], x.em.contents
输出:
Full forecast for Chicago, Illinois, United States (Haze) [u’35…47 °F’]
Full forecast for London, Greater London, England (Light Rain) [u’43…45 °F’]
Full forecast for New York, New York, United States (Partly Cloudy) [u’42…62 °F’]
Full forecast for San Francisco, California, United States (Partly Cloudy) [u’51…70 °F’]