使用python,requests和bs4的亚马逊价格网页抓取
我对网上搜寻亚马逊文章的价格存在疑问.我试图获取某件商品的价格,但不幸的是,它并非总是能正常工作.我随机得到状态码503(服务器不可用).我可以通过while循环解决该问题,如果状态码== 200,则该循环结束.我想了解服务器不可用的主要问题,因此我可以解决主要问题而不进行解决.到目前为止,该问题仅在亚马逊上发生.
I have a question about web scraping the price of an amazon article. I tried to get the price of an article and it works unfortunately not always. I get randomly the status code 503 (server unavailable). I can work around that problem with a while loop that ends if status code == 200. I want to understand the main problem of the unavailable server, so I can maybe fix the main problem and not work around of it. The problem occurs only on amazon so far.
这是我10次测试的代码.请求通常失败2/10次
here is my code for a 10 times test. The request usually fails 2/10 times
import requests
from bs4 import BeautifulSoup
for i in range(10):
page = requests.get("https://www.amazon.de/Bloodborne-Game-Year-PlayStation-4/dp/B016ZU4FIQ/ref=sr_1_3?ie=UTF8&qid=1519566642&sr=8-3&keywords=bloodborne+ps4")
if page.status_code != 200:
print("Error status code: " + str(page.status_code))
continue
soup = BeautifulSoup(page.content, "html.parser")
price = soup.find(id="priceblock_ourprice", class_="a-size-medium a-color-price")
price_string = price.get_text()
print(price_string)
尝试以下脚本.它应该可以为您带来价格.
Try the below script. It should get you the price.
import requests
from bs4 import BeautifulSoup
URL = "https://www.amazon.de/Bloodborne-Game-Year-PlayStation-4/dp/B016ZU4FIQ/ref=sr_1_3?ie=UTF8&qid=1519566642&sr=8-3&keywords=bloodborne+ps4"
page = requests.get(URL,headers={"User-Agent":"Defined"})
soup = BeautifulSoup(page.content, "html.parser")
price = soup.find(id="priceblock_ourprice").get_text()
print(price)
输出:
EUR 34,99