使用Python(城市)作为参数进行Web Scraping

时间:2017-03-29 18:23:46

标签: python web web-scraping screen-scraping jython

def findWeather(city):
  import urllib

  connection = urllib.urlopen("http://www.canoe.ca/Weather/World.html")
  rate = connection.read()
  connection.close()
  currentLoc = rate.find(city)
  curr = rate.find("currentDegree")
  temploc = rate.find("</span>", curr)
  tempstart = rate.rfind(">", 0, temploc)
  print "current temp:", rate[tempstart+1:temploc]

上面提供了链接。我遇到的问题是我每次运行程序并使用,比如&#34;布鲁塞尔&#34;在比利时,作为参数,即findWeather(&#34;布鲁塞尔&#34;),它将始终打印24c作为温度,而(正如我写的那样)它应该是19c。这是该网站提供的许多其他城市的情况。欢迎提供此代码的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

这个应该有效:

import requests
from bs4 import BeautifulSoup
url = 'http://www.canoe.ca/Weather/World.html'
response = requests.get(url)
# Get the text of the contents
html_content = response.text
# Convert the html content into a beautiful soup object
soup = BeautifulSoup(html_content, 'lxml')
cities = soup.find_all("span", class_="titleText")

cels = soup.find_all("span", class_="currentDegree")

for x,y in zip(cities,cels):
    print (x.text,y.text)