首页 > 解决方案 > 抓取 div 属性不起作用。无类型错误

问题描述

import bs4

# Open urlopen function in request module in urllib library
from urllib.request import urlopen as user_request
from bs4 import BeautifulSoup as soup

my_url = 'https://www.newegg.com/p/pl?N=100160979%20601305587'

# open up connection and grab html content from url#
user_client = user_request(my_url)

# load content onto variable
page_html = user_client.read()

# close the client
user_client.close()

page_soup = soup(page_html, "html.parser")

# find all products on page using their class tag
containers = page_soup.findAll("div",{"class":"item-branding"})

# find all brands
for container in containers:
    brand = container.a.img["title"]

# find all product titles
    title_container = container.findAll("a", {"class" : "item-title"})
    product_name = title_container[0].text

    print("brand: " + brand)
    print("product_name: " + product_name)

我正在尝试使用 container.div.div.a.img["title"] 在本教程中使用的 21:15 https://www.youtube.com/watch?v刮取 title = "LG Electronics" =XQgXKtPSzUI

鉴于此处找到的容器的内容,我不知道为什么,但它给了我以下错误。回溯(最后一次调用):文件“”,第 1 行,在 AttributeError 中:“NoneType”对象没有属性“a”

任何帮助,将不胜感激。

更新:

我发现使用 {"class":"item-branding"} 修复了这个错误,但现在我在第 29 行 "product_name = title_container[0].text" 上遇到了一个问题

问题:

文件“monitorscrape.py”,第 29 行,在 product_name = title_container[0].text IndexError: list index out of range

任何帮助,将不胜感激。谢谢。

标签: pythonhtmlweb-scrapingbeautifulsoup

解决方案


产品可能没有任何品牌,因此您需要照顾它。

例如:

# Open urlopen function in request module in urllib library
from urllib.request import urlopen as user_request
from bs4 import BeautifulSoup as soup

my_url = 'https://www.newegg.com/p/pl?N=100160979%20601305587'

# open up connection and grab html content from url#
user_client = user_request(my_url)

# load content onto variable
page_html = user_client.read()

# close the client
user_client.close()

page_soup = soup(page_html, "html.parser")

for container in page_soup.select('.item-container'):
    brand = container.select_one('.item-branding img')
    brand = brand['title'] if brand else '- NO BRAND -'

    title = container.select_one('.item-title').get_text(strip=True)

    print('Brand: {}'.format(brand))
    print('Title: {}'.format(title))
    print('-' * 160)

印刷:

Brand: Westinghouse
Title: Westinghouse WH32UX9019 32" Ultra HD 3840 x 2160 4K Resolution 60Hz 8ms 2xHDMI DisplayPort Flicker-Free Low Blue Light Filter Frameless Design Widescreen LED Backlit LCD Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27BL65U-W 27" 3840x2160 4K UHD IPS LED LCD HDR 16:9 60Hz FreeSync Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27UD58-B IPS 4K UHD Free-Sync Gaming Monitor, 3840 x 2160, 5ms Response Time, 1000:1 Contrast Ratio, DisplayPort, HDMI, Tilt / Height Adjustable, VESA Compatible
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: Acer America
Title: Acer ET322QK wmiipx 32" (Actual size 31.5") Ultra HD 3840 x 2160 4K 4ms (GTG) 60Hz 2 x HDMI DisplayPort AMD FreeSync Built-in Speakers Backlit LED LCD Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: SAMSUNG
Title: Samsung UR59C Series U32R590C 32" (Actual size 31.5") Ultra HD 3840 x 2160 4K Resolution 4ms (GTG) HDMI DisplayPort Flicker Free LED Backlit Curved LCD Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: ASUS
Title: ASUS MX27UCS 27" UHD 3840 x 2160 (4K) 5ms (GTG) 75 Hz HDMI, DisplayPort, USB-C Built-in Speakers Eye Care Monitor, Frameless, Flicker Free, Blue Light Filter, Anti Glare
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: BenQ
Title: BenQ Entertainment EW2780U 27" Ultra HD 3840 x 2160 4K 5ms 2xHDMI USB Type-C DisplayPort Built-in Speakers Flicker-Free Low Blue-Light HDRi Backlit LED IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: HP
Title: HP Z27 2TB68A8#ABA 27" UHD 3840 x 2160 (4K) 8ms (GTG) 60 Hz HDMI, DisplayPort, USB IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27BL85U-W 27" 4K Ultra HD 3840 x 2160 HDMI DisplayPort USB Type-C FreeSync Tilt, Pivot, Height Adjustable IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: BenQ
Title: BenQ DesignVue Designer PD2700U 27" Ultra HD 3840 x 2160 4K Resolution 5ms (GTG) DisplayPort, HDMI, Mini-DisplayPort Low Blue Light Filcker-Free USB Hub Built-in Speakers LED Backlit IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27BK67U-B 27" 16:9 4K UHD 3840 x 2160 IPS Monitor with sRGB 99% and AMD FreeSync HDMI, DisplayPort, USB-C
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: SAMSUNG
Title: SAMSUNG U28E590D Black 28" 4K UHD Widescreen LCD/LED Monitor, AMD FreeSync 1ms, 370 cd/m2 DCR Mega Infinity (1000:1), VESA Mountable, HDMI DisplayPort
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: DELL
Title: Dell U2718Q 27" 16:9 UltraSharp InfinityEdge 4K UHD 3840x2160 IPS HDR 350 cd/m² Monitor DP, Mini DP, HDMI, USB 3.0 HUB, Height Adjustment Pivot Swivel
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: Philips
Title: Philips 328E1CA 32" 3840x2160 4K UHD 4ms Frameless Curved Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 43UN700-B 43" Ultra HD 3840 x 2160 4K 8ms (GTG) 60Hz 4xHDMI DisplayPort USB Type-C Built-in Speakers Flicker Safe PBP PIP Anti-Glare Backlit LED IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: BenQ
Title: BenQ Entertainment EW3280U 32" 4K Ultra HD 3840 x 2160 5ms 2 x HDMI, DisplayPort, USB Type-C Built-in Speakers Flicker-Free Low Blue Light FreeSync WLED IPS Gaming Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 32BL75U 32" Ultra HD 4K 3840 x 2160 4ms 2 x HDMI, DisplayPort, USB Type-C Display HDR 600 Built-in Speakers AMD Radeon FreeSync LED Gaming Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27UD68-P 27" FreeSync IPS LED Monitor 4K UHD 3840 x 2160 16:9 Widescreen 5ms (GTG) On-Screen Control with Screen Split Game Mode & Black Stabilizer HDMI DisplayPort
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: Viotek
Title: VIOTEK NV32Q True 4K Monitor 32-Inch Curved | 60Hz 4ms (OD) Streaming-Ready 3840 x 2160p Monitor for Gaming/Movies | HDR-Ready 1500R VA Panel w/ FreeSync | HDMI 2.0 DP 1.2 Audio Out (VESA)
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: HP
Title: HP Envy 27 27" Ultra HD 3840 x 2160 4K Resolution 5ms (OD GTG) 2x HDMI, DisplayPort, USB Type-C AMD FreeSync Anti-Glare Widescreen LED Backlight IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: HP
Title: HP Z32 32" (Actual size 31.5") Ultra HD 3840 x 2160 4K Resolution DisplayPort HDMI Mini-DisplayPort USB Type-C Anti-Glare LED Backlit IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: DELL
Title: Dell P2415Q 24" (Actual size 23.8") Ultra HD 3840 x 2160 4K 6ms (GTG) HDMI DisplayPort Mini-DisplayPort USB 3.0 Hub MHL Compatible Anti-Glare Backlit LED IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: ASUS
Title: ASUS Designo MX27UC 27" 3840 x 2160 (4K) 5ms (GTG) HDMI, DisplayPort Built-in Speakers LCD Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27BL55U-B 27" 4K UHD LCD Monitor - 16:9 - TAA Compliant
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: Acer America
Title: Acer B286HK ymjdpprz UM.PB6AA.003 28" UHD 3840 x 2160 (4K) 2 ms 60 Hz DVI, HDMI, DisplayPort Built-in Speakers LED Backlight Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: Lenovo
Title: Lenovo ThinkVision P32u-10 32" WLED LCD Monitor - 16:9 - 6 ms
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: DELL
Title: Dell UltraSharp U2720Q 27" 3840 x 2160 4K 60Hz IPS 16:9 DisplayHDR 400 HDMI, DisplayPort, USB-C Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: DELL
Title: DELL UltraSharp 31.5" 4K Ultra HD 3840x2160 5 ms USB-C IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 32UD99-W Black 32" 4K HDR10 IPS Widescreen Monitor FreeSync 5ms GTG 2 x 5W Speakers VESA USB Type-C USB 3.0 DisplayPort HDMI
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: SAMSUNG
Title: SAMSUNG U32J590UQN 32" 3840 x 2160 (4K) 4(GTG) ms LCD/LED Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: HP
Title: HP Z43 (Actual size 42.5") Ultra HD 3840 x 2160 4K 8ms DisplayPort HDMI Mini-DisplayPort USB Type-C Anti-Glare USB Hub LED Backlit IPS Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: ViewSonic
Title: ViewSonic VG2860MHL-4K 28" Quad HD 3840 x 2160 4K 2ms 2xHDMI 2xDisplayPort DVI-D USB Hub Built-in Speakers Anti-Glare Backlit LED Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: - NO BRAND -
Title: 15.6" IPS 4K FHD 3840x2160 HDR Portable Monitor with Speaker/TYPE C/HDMI/Micro USB Ports/Leather Case for PS4/Xbox One/Switch/Phone/Laptop/Industrial & Medical Display etc.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: Acer America
Title: Acer B276HK Bymjdpprzx UM.HB6AA.B03 27" 4K UHD 3840 x 2160 (4K) 5 ms GTG DVI, HDMI, DisplayPort Built-in Speakers LCD/LED Monitor
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27MU88-W Black Texture Front Deco, Silver Spray Middle 27" Class 4K UHD AMD FreeSync IPS Monitor, sRGB 99%, HDMI, USB3.0, Display Port,USB-C, Tilt, Height Adjustable, Pivot, VESA Compatible
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Brand: LG Electronics
Title: LG 27UL500-W Black / Silver 27" 5ms (GTG) HDMI Widescreen sRGB 98% 4K UHD 3840 x 2160 HDR Monitor w/ Radeon FreeSync
----------------------------------------------------------------------------------------------------------------------------------------------------------------

推荐阅读