我今天在玩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