我在尝试爬取的每个网站中都不断收到”None”。

huangapple go评论98阅读模式
英文:

I keep getting "None" in beautiful soup for every website that I try to scrape

问题

我只会翻译文本部分,不会翻译代码。以下是您提供的文本的翻译:

我只是尝试做一个简单的天气应用程序,我试图通过获取天气标签来抓取开放天气,但它一直报错

AttributeError: 'NoneType' object has no attribute 'span'

如果我删除 .span,它只会显示 None,这也不是我想要的。
我希望能够打印出 HTML 标签。我不确定出了什么问题,而且如果我删除 span,它也只会显示 none。

代码:

from bs4 import BeautifulSoup
import requests
import re

url = "https://openweathermap.org/city/4930956" # 从中获取天气的城市的 URL

page = requests.get(url).text # 使用 requests 获取页面内容
doc = BeautifulSoup(page,"html.parser") # 使用 BeautifulSoup 解析文档

temperature = doc.find(class_="heading") # 获取位于 heading 类中的温度值,该温度值位于 span 标签中

print(temperature) # 打印标签
英文:

I was just trying to do a simple weather app and I was trying the scrape open weather by getting the tag of the weather but it kept saying

AttributeError: 'NoneType' object has no attribute 'span'

If I remove the .span it just says None which is also not what I want.
I was expecting to get the html tag printed out. I'm not sure what's going wrong, also if I remove the span it just says none.

code:

from bs4 import BeautifulSoup
import requests
import re

url = "https://openweathermap.org/city/4930956" #url for the city that im getting the weeather from

page = requests.get(url).text #using requets
doc = BeautifulSoup(page,"html.parser") #reading the document with beautifulsoup

temperature = doc.find(class_="heading") #getting the temperature value which is in the heading class with the span tag

print(temperature) #printing the tag

答案1

得分: 2

这并不完全回答你的问题,但他们有一个API https://openweathermap.org/api
看起来这个API可以免费使用,每天少于一千次查询。

你可能会遇到一些防止爬取的问题,因为他们希望人们要么通过API访问他们的数据,要么像普通人一样浏览他们的网站。

由于我声望不够高,无法回答你的问题哈哈。

英文:

This doesn't answer your question exactly, but they have an api https://openweathermap.org/api
Looks like the api is free to use for less than a thousand queries per day.

You might be experiencing some anti-scraping stuff from them, as they want people to access their data either through the api or as a normal person looking at the website.

Can't reply to your question as I don't have enough rep lol

huangapple
  • 本文由 发表于 2023年8月10日 11:19:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定