以Python(city)为参数的网页抓取

2024-09-28 20:47:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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]

上面提供了链接。我遇到的问题是,每当我运行程序并使用比利时的“布鲁塞尔”作为参数,即findWeather(“布鲁塞尔”),它总是打印24摄氏度作为温度,而(在我写这篇文章时)它应该是19摄氏度。这是网站提供的许多其他城市的情况。在此代码上的帮助将不胜感激。你知道吗

谢谢!你知道吗


Tags: importhttpcityratedefwwwfindurllib
1条回答
网友
1楼 · 发布于 2024-09-28 20:47:13

这个应该有用:

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)

相关问题 更多 >