【发布时间】:2016-08-01 22:21:13
【问题描述】:
我刚开始学习 Python 就遇到了这个问题。能够从亚马逊解析价格并将其打印到控制台。
这是我的代码:
import requests, bs4
def getAmazonPrice(productUrl):
res = requests.get(productUrl)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
elems = soup.select('#addToCart > a > h5 > div > div.a-column.a-span7.a-text-right.a-span-last > span.a-size-medium.a-color-price.header-price')
return elems[0].text.strip()
price = getAmazonPrice('http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994/ref=sr_1_2?ie=UTF8&qid=1460386052&sr=8-2&keywords=python+book')
print('The price is ' + price)
错误信息:
Traceback(最近一次调用最后一次):文件 “D:/Code/Python/Basic/webBrowser-Module.py”,第 37 行,在 价格 = getAmazonPrice('http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994/ref=sr_1_2?ie=UTF8&qid=1460386052&sr=8-2&keywords=python+book') 文件“D:/Code/Python/Basic/webBrowser-Module.py”,第 30 行,在 获取亚马逊价格 res.raise_for_status() 文件“C:\Python33\lib\requests\models.py”,第 844 行,在 raise_for_status 引发 HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 503 Server Error: Service Unavailable 对于网址: http://www.amazon.com/Automate-Boring-Stuff-Python-Programming/dp/1593275994/ref=sr_1_2?ie=UTF8&qid=1460386052&sr=8-2&keywords=python+book
进程以退出代码 1 结束
【问题讨论】:
-
我收到同样的错误信息。 __requests.exceptions.HTTPError: 503 Server Error: Service Unavailable for url __ 即使按照建议操作
标签: python request beautifulsoup