【发布时间】:2017-01-23 07:41:03
【问题描述】:
我正在尝试从亚马逊获取一些数据,我的代码是:
import requests, bs4
source_code = requests.get("https://www.amazon.com/s/ref=sr_nr_p_n_feature_keywords_0?fst=as%3Aoff&rh=n%3A2335752011%2Cn%3A%212335753011%2Cn%3A7072561011%2Cn%3A2407749011%2Cp_89%3AHuawei%2Cp_n_feature_keywords_four_browse-bin%3A6787346011&bbn=2407749011&ie=UTF8&qid=1473923594&rnid=6787345011",
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36"
})
source_code.raise_for_status()
soup = bs4.BeautifulSoup(source_code.text, 'lxml')
mobile_div = soup.find_all("div", class_="a-row a-spacing-small")
for mobile_name in mobile_div:
print(mobile_name.a.find_next("h2").string)
它输出很好,但是当我使用时
print(mobile_name.a.h2.string)
而不是显示以下错误:
print(mobile_name.a.h2.string)
AttributeError: 'NoneType' object has no attribute 'string'
谁能解释我为什么会收到这个错误?
【问题讨论】: