【问题标题】:Python bs4 find_all() not showing me text I looked up in HTMLPython bs4 find_all() 没有显示我在 HTML 中查找的文本
【发布时间】:2017-06-23 17:29:33
【问题描述】:

我使用 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 但在控制台中什么也没有,但我什么也没得到。

【问题讨论】:

  • link2 有效。其他链接只是不正确的选择器。

标签: python html beautifulsoup python-requests


【解决方案1】:

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

tag[attribute='attribute_value']

阅读更多BS Documentation

【讨论】:

  • 感谢不吝赐教
猜你喜欢
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多