Python bs4 find_all()没有显示我在HTML

时间:2017-06-23 17:29:34

标签: python html beautifulsoup python-requests

我使用Python BeautifulSoup4和Requests从这个网站获取数据:http://www.weather.gov/ctp/。我希望我的程序从 website返回文本,但我的输出中没有任何内容。

这是我的代码:

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

我尝试使用相同的方法从网站的其他部分检索文本并且它有效。我遇到了另一个网站同样的问题,但这次是一个数字。我尝试将该数字检索为可以分配给变量的值,尽管事实上在测试输出值后我得到了True,但在控制台中却没有任何内容。

1 个答案:

答案 0 :(得分:0)

  

soup.select( '一个[HREF = “http://www.weather.gov”]')

tag[attribute='attribute_value']

BS Documentation

了解详情