我今天在玩BeautifulSoup和Requests API。所以我想我会写一个简单的刮板,它会跟随深度为2的链接(如果有意义的话)。我所刮的网页中的所有链接都是相对的。 (例如:<a href="/free-man-aman-sethi/books/9788184001341.htm" title="A Free Man">
)所以为了使它们绝对,我以为我会加入页面的网址与相关链接使用urljoin
。TypeError:在BeautifulSoup中使用Python进行分割时无法调用'NoneType'对象
要做到这一点,我不得不首先从<a>
标签提取href值和,我想我会用split
:
#!/bin/python
#crawl.py
import requests
from bs4 import BeautifulSoup
from urlparse import urljoin
html_source=requests.get("http://www.flipkart.com/books")
soup=BeautifulSoup(html_source.content)
links=soup.find_all("a")
temp=links[0].split('"')
这提供了以下错误:
Traceback (most recent call last):
File "test.py", line 10, in <module>
temp=links[0].split('"')
TypeError: 'NoneType' object is not callable
有在正确地浏览文档之前先深入了解,我意识到这可能不是实现我的目标的最佳方式,但为什么会出现TypeError?
子元素,而不是属性。 – 2013-03-14 12:26:32