Python 3.x 使用BeautifulSoup在amazon页面的所有span标记中查找文本

Python 3.x 使用BeautifulSoup在amazon页面的所有span标记中查找文本,python-3.x,web-scraping,beautifulsoup,Python 3.x,Web Scraping,Beautifulsoup,我试图解析span标记中的所有文本。但是当我尝试下面的代码时没有输出 import requests from bs4 import BeautifulSoup r=requests.get('https://www.amazon.in/s?k=phones&ref=nb_sb_noss_2') c=r.content soup=BeautifulSoup(c,'html5lib') for s in soup.find_all('span',class_='a-size-medium

我试图解析span标记中的所有文本。但是当我尝试下面的代码时没有输出

import requests
from bs4 import BeautifulSoup
r=requests.get('https://www.amazon.in/s?k=phones&ref=nb_sb_noss_2')
c=r.content
soup=BeautifulSoup(c,'html5lib')
for s in soup.find_all('span',class_='a-size-medium a-color-base a-text-normal'):
   print(s.text)

请提供帮助。

添加
用户代理
HTTP头以获得正确响应:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}

r=requests.get('https://www.amazon.in/s?k=phones&ref=nb_sb_noss_2', headers=headers)
c=r.content
soup=BeautifulSoup(c,'html5lib')
for s in soup.find_all('span',class_='a-size-medium a-color-base a-text-normal'):
   print(s.text)
印刷品:

Vivo V19 (Mystic Silver, 8GB RAM, 128GB Storage) with No Cost EMI/Additional Exchange Offers
Redmi Note 8 (Neptune Blue, 4GB RAM, 64GB Storage)
Redmi 8A Dual (Sea Blue, 2GB RAM, 32GB Storage) – Dual Cameras & 5,000 mAH Battery
Samsung Galaxy M31 (Ocean Blue, 6GB RAM, 128GB Storage)
OPPO A5 2020 (Dazzling White, 3GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange Offers
itel A46 (Neon Water, 2GB RAM, 16GB Storage)
Samsung Galaxy M21 (Raven Black, 6GB RAM, 128GB Storage)
I Smart is 58 4G Smartphone (Jio 4G Sim Not Supported) 2GB RAM with 5.5 Inch Display and 16GB ROM 4G Mobile (Blue)
Honor 9X (Sapphire Blue, 6+128GB Storage) -Pop up Front Camera & 48MP Triple Rear Camera
OPPO A12 (Black, 3GB RAM, 32GB Storage) with No Cost EMI/Additional Exchange Offers
I Kall K400 (4GB RAM, 64GB Storage) (Blue)
Samsung Galaxy M31 (Ocean Blue, 6GB RAM, 64GB Storage)
Vivo U10 (Electric Blue, 5000 mAH 18W Fast Charge Battery, 3GB RAM, 32GB Storage)
I Kall K1 Smartphone (8GB, Dual Sim 4G Volte) (Gold)
OPPO A5 2020 (Mirror Black, 3GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange Offers
Redmi Note 8 (Moonlight White, 4GB RAM, 64GB Storage)
Samsung Galaxy M21 (Midnight Blue, 4GB RAM, 64GB Storage)
Vivo Y12 (Aqua Blue, 3GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange Offers
OnePlus 7T (Glacier Blue, 8GB RAM, Fluid AMOLED Display, 256GB Storage, 3800mAH Battery)
Vivo Y30 (Emerald Black, 4GB RAM, 128GB Storage) with No Cost EMI/Additional Exchange Offers
OPPO Reno3 Pro (Midnight Black, 8GB RAM, 128GB Storage) with No Cost EMI/Additional Exchange Offers

添加
User-Agent
HTTP头以获得正确的响应:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}

r=requests.get('https://www.amazon.in/s?k=phones&ref=nb_sb_noss_2', headers=headers)
c=r.content
soup=BeautifulSoup(c,'html5lib')
for s in soup.find_all('span',class_='a-size-medium a-color-base a-text-normal'):
   print(s.text)
印刷品:

Vivo V19 (Mystic Silver, 8GB RAM, 128GB Storage) with No Cost EMI/Additional Exchange Offers
Redmi Note 8 (Neptune Blue, 4GB RAM, 64GB Storage)
Redmi 8A Dual (Sea Blue, 2GB RAM, 32GB Storage) – Dual Cameras & 5,000 mAH Battery
Samsung Galaxy M31 (Ocean Blue, 6GB RAM, 128GB Storage)
OPPO A5 2020 (Dazzling White, 3GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange Offers
itel A46 (Neon Water, 2GB RAM, 16GB Storage)
Samsung Galaxy M21 (Raven Black, 6GB RAM, 128GB Storage)
I Smart is 58 4G Smartphone (Jio 4G Sim Not Supported) 2GB RAM with 5.5 Inch Display and 16GB ROM 4G Mobile (Blue)
Honor 9X (Sapphire Blue, 6+128GB Storage) -Pop up Front Camera & 48MP Triple Rear Camera
OPPO A12 (Black, 3GB RAM, 32GB Storage) with No Cost EMI/Additional Exchange Offers
I Kall K400 (4GB RAM, 64GB Storage) (Blue)
Samsung Galaxy M31 (Ocean Blue, 6GB RAM, 64GB Storage)
Vivo U10 (Electric Blue, 5000 mAH 18W Fast Charge Battery, 3GB RAM, 32GB Storage)
I Kall K1 Smartphone (8GB, Dual Sim 4G Volte) (Gold)
OPPO A5 2020 (Mirror Black, 3GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange Offers
Redmi Note 8 (Moonlight White, 4GB RAM, 64GB Storage)
Samsung Galaxy M21 (Midnight Blue, 4GB RAM, 64GB Storage)
Vivo Y12 (Aqua Blue, 3GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange Offers
OnePlus 7T (Glacier Blue, 8GB RAM, Fluid AMOLED Display, 256GB Storage, 3800mAH Battery)
Vivo Y30 (Emerald Black, 4GB RAM, 128GB Storage) with No Cost EMI/Additional Exchange Offers
OPPO Reno3 Pro (Midnight Black, 8GB RAM, 128GB Storage) with No Cost EMI/Additional Exchange Offers

谢谢@AndrejKeseley谢谢@AndrejKeseley