【问题标题】:Extracting value of specific tag by BeautifulSoup in python在python中通过BeautifulSoup提取特定标签的值
【发布时间】:2014-09-25 12:18:22
【问题描述】:

我有一个简单的 python 脚本,例如:

#!/usr/bin/python
import requests
import BeautifulSoup
response = requests.get('http://site.ir/')
out=response.content

soup = BeautifulSoup.BeautifulSoup(out)
for anchor in soup.select('body a'):
    print anchor.string

但是出现以下错误:

  File "p.py", line 11, in <module>
for anchor in soup.select('body a'):
TypeError: 'NoneType' object is not callable

图片:

【问题讨论】:

  • 我将删除我的答案,因为您一直在更改问题;它不再有效。您现在正在使用 BeautifulSoup 3 尝试使用 BeautifulSoup 4 功能。
  • 你看过返回的内容了吗?
  • @Martijn Pieters 谢谢...你能提出新的答案吗?我不知道 3 和 4 之间的区别,请原谅让您忙...
  • @Padraic Cunningham 它返回了你在帖子中看到的错误,
  • 我的意思是你的html返回了,我不认为它返回你认为的那样

标签: python beautifulsoup


【解决方案1】:

安装 BeautifulSoup4 并执行以下操作:

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get('http://site.ir/').content)
for anchor in soup.select('body a'):
    print anchor.text

【讨论】:

    猜你喜欢
    • 2013-07-01
    • 2020-05-02
    • 2018-12-12
    • 1970-01-01
    • 2021-07-08
    • 2017-09-21
    • 2014-11-23
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多