簡體   English   中英

無法使用 python 從網站打印文本

[英]Unable to print text from website using python

因為它只給我一個關於品牌的錯誤,我應該如何解決它

回溯(最近一次調用):文件“d:/python/davago.py”,第 15 行,在brand= property.find('h3', class_ = 'productitem--vendor').text.strip() AttributeError 中: 'NoneType' 對象沒有屬性 'text'

import requests
from bs4 import BeautifulSoup
from bs4 import *
import pandas as pd
import time

url = 'https://dvago.pk/collections/cardio-vascular-system?page=1&grid_list=grid-view'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
content = soup.find_all('div', class_ = 'productitem')
#print(content)
for property in content:
    names= property.find('div', class_ = 'productitem--info')
    name= names.find('h2', class_ = 'productitem--title').text.strip()
    brand= property.find('h3', class_ = 'productitem--vendor').text.strip()
    

    print(name,brand)

如果您看到最后三個內容沒有供應商名稱,那么您可以使用tryexcept塊來處理exception並打印適當的語句

import requests
from bs4 import BeautifulSoup
from bs4 import *
import pandas as pd
import time

url = 'https://dvago.pk/collections/cardio-vascular-system?page=1&grid_list=grid-view'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
content = soup.find_all('div', class_ = 'productitem')
#print(content)
for property in content:
    names= property.find('div', class_ = 'productitem--info')
    try:
        name= names.find('h2', class_ = 'productitem--title').text.strip()
    except AttributeError:
        name="Company name not available"
    try:
        brand= property.find('h3', class_ = 'productitem--vendor').text.strip()
    except AttributeError:
        brand="Brand name not available"
    

    print("Company Name:",name)
    print("Brand Name:",brand)

您應該先找到“productgrid--items”,然后再查看其中的每個項目。 'productitem' 類可能還有其他用途,但其中沒有 h3,這就是您收到異常的原因:

import requests
from bs4 import BeautifulSoup
from bs4 import *
# import pandas as pd
# import time

url = 'https://dvago.pk/collections/cardio-vascular-system?page=1&grid_list=grid-view'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
aaa = soup.find('div', class_='productgrid--items') ## <------
content = aaa.find_all('div', class_='productitem') ## <------
# print(content)
for property in content:
    names = property.find('div', class_='productitem--info')
    name = names.find('h2', class_='productitem--title').text.strip()
    brand = property.find('h3', class_='productitem--vendor').text.strip()

    print(name, brand)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM