Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Beautiful Soup有时无法从亚马逊获得价格_Python_Beautifulsoup_Python Requests_Css Selectors_Http Headers - Fatal编程技术网

Python Beautiful Soup有时无法从亚马逊获得价格

Python Beautiful Soup有时无法从亚马逊获得价格,python,beautifulsoup,python-requests,css-selectors,http-headers,Python,Beautifulsoup,Python Requests,Css Selectors,Http Headers,在运行Beauty soup脚本时,为了从amazon获取价格,我遇到了一个问题,Beauty soup常常无法以输出中的空列表的形式随机获取价格 def getAmazonPrice(产品URL): 元素=[] 当元素==无或元素=[]时: headers={'User-Agent':'Mozilla/5.0(WindowsNT6.3;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/54.0.2840.71 Safari/537.36'}让服务

在运行Beauty soup脚本时,为了从amazon获取价格,我遇到了一个问题,Beauty soup常常无法以输出中的空列表的形式随机获取价格

def getAmazonPrice(产品URL):
元素=[]
当元素==无或元素=[]时:
headers={'User-Agent':'Mozilla/5.0(WindowsNT6.3;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/54.0.2840.71 Safari/537.36'}让服务器认为它是一个网络浏览器而不是机器人
res=requests.get(productUrl,headers=headers)
res.为_状态提高_()
soup=bs4.BeautifulSoup(res.text,“lxml”)
elems=soup。选择(“#mediaNoAccordion>div.a-row>div.a-column.a-span4.a-text-right.a-span-last>span.a-size-medium.a-color-price.header price”)
打印(元素)
返回元素[0]。text.strip()
价格=getAmazonPrice('https://www.amazon.com/Automate-Boring-Stuff-Python-2nd-ebook/dp/B07VSXS4NK/ref=sr_1_1?crid=30NW5VCV06ZMP&dchild=1&keywords=automate+使用+python&qid=1586810720&sprefix=automatic+bo%2Caps%2C288&sr=8-1')
打印('价格为'+价格)
输出:

[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[<span class="a-size-medium a-color-price header-price">


                        $26.58



        </span>]
The price is $26.58
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[
$26.58
]
价格是26.58美元

只需将
res.text
保存到html文件,您就会看到您被captcha阻止。

您之前已经收到有关问题的详细答案。使用
API
或仅使用
selenium
但有时我是如何通过验证码的,并且它返回了价格>?这是一个很好的问题,但你必须问问亚马逊,他们什么时候用验证码阻止请求的。即使是真正的用户有时也会得到验证码: