尽管存在价值,美丽的汤什么也没有给

时间:2019-05-16 12:17:25

标签: python beautifulsoup

为什么我的以下代码给出了输出none

NONE

我尝试使用from bs4 import BeautifulSoup import urllib3 import warnings warnings.filterwarnings("ignore", category=FutureWarning) urllib3.disable_warnings() url = "https://www.amazon.ae/dp/B07N62ZGWQ/ref=br_msw_pdt-5?_encoding=UTF8&smid=ABO0A2K2SKD5O&pf_rd_m=A2KKU8J8O8784X&pf_rd_s=&pf_rd_r=GZ376KPZWEXC0TBHXPKY&pf_rd_t=36701&pf_rd_p=da8f558b-6ee9-4705-8e31-6a4c345b29e8&pf_rd_i=desktop" http = urllib3.PoolManager() r = http.request('GET', url) content = r.data.decode('utf-8') soup = BeautifulSoup(content,'html.parser') print(soup.find('span', {'id':"priceblock_ourprice"})) soup.find(id="priceblock_ourprice"),但都没有输出。

ID显示在页面上,并且也有一个值。

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,我将urllib3更改为requests,因为urllib3给出了例外情况

请参见下面的代码:

import requests
from bs4 import BeautifulSoup

url = "https://www.amazon.ae/dp/B07N62ZGWQ/ref=br_msw_pdt-5?_encoding=UTF8&smid=ABO0A2K2SKD5O&pf_rd_m=A2KKU8J8O8784X&pf_rd_s=&pf_rd_r=GZ376KPZWEXC0TBHXPKY&pf_rd_t=36701&pf_rd_p=da8f558b-6ee9-4705-8e31-6a4c345b29e8&pf_rd_i=desktop"

response = requests.request('GET', url)
content = response.content.decode('utf-8')
soup = BeautifulSoup(content, 'html.parser')
table = soup.find('table', attrs={'class': 'a-lineitem'})
price_blocks = table.find_all('span', attrs={'id': 'priceblock_ourprice'})
prices = [block.text for block in price_blocks]
print(prices)

输出控制台:

['AED26.00']