python bs4 find_all()没有向我展示我在html中查找的文字



我使用了Python BeautifulSoup4,并请求从此网站获取数据:http://www.weather.gov/ctp/。我希望我的程序从网站上返回文本,但我的输出一无所获。

这是我的代码:

import requests as r
from bs4 import BeautifulSoup as bs
doc = r.get("http://www.weather.gov/ctp/")
soup = bs(doc.content, "html.parser")
# I tried all of these methods to get "Weather.gov" as a returned string but none of them worked!
link1 = soup.find_all('href="http://www.weather.gov"')
link2 = soup.find_all("a", {'href':'http://www.weather.gov'})
link3 = soup.select('href["http://www.weather.gov"]')
for item in link3:
    print item.contents
# This loop does not return anything in the console

我尝试使用相同的方法从网站的另一部分检索文本,并且起作用。我在另一个网站上遇到了同样的问题,但是这次有一个数字。我试图将该数字检索为可以分配给变量的值,尽管在测试了一个值之后,我什么也没得到,但我将获得真实,但在控制台中没有任何东西。

soup.select('a [href =" http://www.weather.gov"]'(

tag[attribute='attribute_value']

在BS文档中阅读更多信息

最新更新