在python中通过BeautifulSoup提取特定标记的值

时间:2014-09-25 12:18:23

标签: python beautifulsoup

我有一个简单的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

图片 enter image description here

1 个答案:

答案 0 :(得分:0)

安装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