python:beautifulsoup4:报废:无法找出错误

时间:2017-01-02 09:18:41

标签: beautifulsoup python-3.6

以下是我编写的代码,用于查找snapdeal.com页面中的所有产品名称

代码:

import urllib.request
   from bs4 import BeautifulSoup as BS

   url = 'https://www.snapdeal.com/products/electronics-headphones sort=plrty'

   response = urllib.request.urlopen(url).read()
   soup = BS(response, 'html.parser')
   product_name={}
   i=0
   for title in soup.find_all('p', class_="product-title"):
           product_name[i]=title.string
           i += 1
   for i in range(1,21):
       print(product_name[i])

输出:

跟踪(最近一次呼叫最后一次):

摩托罗拉Pulse 2耳机有线麦克风(黑色)

Bose SoundLink头戴式蓝牙耳机 - 黑色&蓝色

Sony MDR-ZX110A无麦克风耳机(白色)

飞利浦SBCHL140 / 98无耳麦克风耳机

文件" C:/ Users / Aritra Bhattacharjee / PycharmProjects / PythonWebModules / Web Scraper.py",第17行,

Intex Desire BT Over Ear与Mic Headphone Black连接

**print(product_name[i])**

带麦克风黑色的JBL T450入耳式有线耳机

KeyError:20

Motorola Pulse Max耳罩式麦克风(黑色)

飞利浦SHB7250WT / 00带麦克风白色的无线耳机

索尼MDR-XB650BT耳机额外低音(XB)耳机,带蓝牙和耳机NFC(黑色)

Intex JAZZ Over Ear有线麦克风耳机黑色

Skullcandy S5GBW-J539带麦克风黑色的耳机无线耳机

JBL C300SI无耳麦克风耳机黑色

带麦克风黑色的Zoook Rocker iFit蓝牙无线耳机

Signature VM-46 Over Ear有线耳机,不带麦克风白色

索尼MDR-G45无线耳机有线耳机 - 黑色

摩托罗拉Pulse Max耳机有麦克风(白色)

适用于三星和三星的Bose SoundTrue耳机配麦克风(海军蓝) Android设备

采用麦克风蓝色的JBL T450入耳式有线耳机

摩托罗拉Pulse 2耳机有线麦克风(白色)

错误行输出粗体。

此外,我还想收集这些产品的价格并将它们并排打印出来。

先谢谢。

1 个答案:

答案 0 :(得分:0)

right_url = 'https://www.snapdeal.com/products/electronics-headphones?sort=plrty 
you_url   = 'https://www.snapdeal.com/products/electronics-headphones sort=plrty'

您错过了网址中的?,如果您想使用索引进行迭代,则可以使用enumerate()

soup = BS(response, 'html.parser')
product_name = [title.string for title in soup.find_all('p',class_="product-title")]
for index, value in enumerate(product_name):
    print(index, value)

出:

0 Sony MDR-ZX110 On Ear Wired Headphone Without Mic White
1 Motorola Pulse 2 Over Ear Wired Headphones With Mic (Black)
2 Bose SoundLink On-Ear Bluetooth Headphones - Black & Blue
3 Sony MDR-ZX110A Headphones Without Mic (White)
4 Philips SBCHL140/98 Over Ear Headphone Without Mic
5 Intex Desire BT Over Ear Wired With Mic Headphone Black
6 JBL T450 On Ear Wired Headphones With Mic Black
7 Motorola Pulse Max Over Ear Wired Headphones With Mic (Black)
8 Philips SHB7250WT/00 Over Ear Wireless Headphones With Mic White
9 Skullcandy S5GBW-J539 On Ear Wireless Headphones With Mic Black
10 boAt Bassheads 600 White & Blue On Ear Wired Headphones With Mic White
11 JBL T450 On Ear Wired Headphones With Mic Blue
12 JBL C300SI Over Ear Wired Without Mic Headphone Black
13 Zoook Rocker iFit Bluetooth Wireless Headphones With Mic Black
14 Intex JAZZ Over Ear Wired With Mic Headphone Black
15 Sony MDR-G45 Over Ear Wired Without Mic Headphone- Black
16 Motorola Pulse Max Over Ear Wired Headphones With Mic (White)
17 Bose SoundTrue Around-Ear Headphones with Mic (Navy Blue) for Samsung and Android Devices
18 Sony MDR-XB650BT On-Ear Extra Bass(XB) Headphones with Bluetooth & NFC (Red)
19 Motorola Pulse 2 Over Ear Wired Headphones With Mic (White)