如何使用美丽的汤中选择该标签中的名称数据?

发布于 2025-01-22 12:08:35 字数 2623 浏览 0 评论 0原文

我是新手编码;我刚开始4个月前。我在Python上使用美丽的汤来尝试提取有关图形卡的信息。

这是我的问题,当我尝试获取图形卡的价格时,它给了我错误的信息。该网站上应花费379.99的卡在航站楼说,价格仅为79.99。所有价格都是终端中的错误数字。我该如何解决?

这是代码:

from bs4.element import PageElement
from bs4 import BeautifulSoup
from matplotlib.colors import LightSource
import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
print("loading website")
webpage_response = requests.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")


print("done loading website")
webpage = webpage_response.content
soup = BeautifulSoup(webpage, "html.parser")
#print(soup)
PageContent = soup.find(attrs={"id": "topPagination"})
MarginLeft = PageContent.find(attrs = {"class": "pages inline"})
CurrentPage = MarginLeft.find(attrs = {"class": "current"})


#helps get graphics card data
Content = soup.find("article", {"id": "productGrid"})
ULinContent = Content.find("ul")
Pwrapper = ULinContent.find_all(attrs={"class": "product_wrapper"})
#print(Pwrapper)
#print(Content)
#print(ULinContent)

CurrentPage = CurrentPage.get_text()
print("We are on page " + CurrentPage)
#print(MarginLeft)
#find number of web pages, then add all the way 
#up to that so we can get all of data
#NumberOfPages = PageContent.find(attrs={"class": "btn"})
#print(NumberOfPages)


#get prices of all rtx cards
prices = []
names = []

links = []
for link in soup.find_all("a"):
    links.append(link.get("href"))
print(links)

def GetCardDetails(prices, Pwrapper, names):
    for Pwrapp in Pwrapper:
        CheckBox = Pwrapp.find(attrs={"class": "checkbox"})
        CheckBoxText = CheckBox.get_text()
        
        #clean card name, the 15 and -11 index cleans compare from html
        CheckBoxClean = CheckBoxText[15:-11]
        #print(CheckBoxClean)
        names.append(CheckBoxClean)
        
        #price
        Card_Price = Pwrapp.find_all(attrs={"itemprop": "price"})
        print(Card_Price)
        print(Pwrapp.get_text())
        for price in Card_Price:
            GetText = price.get_text()
            prices.append(GetText)
            print(GetText)


    DicPrName = {"Graphics_Card_Name": names, "Prices": prices}
    #MAKE GRAPH
    data_frame2 = pd.DataFrame.from_dict(DicPrName)
    print(
        )
    plt.hist(prices)
    plt.show()
    print(DicPrName)

GetCardDetails(prices, Pwrapper, names)
#print(prices)
#print(names)

#average and graph them, get their data









#find the variabels to store using dict and table

I'm new to coding; I just started 4 months ago. I'm using beautiful soup on python to try and extract information about graphics cards.

Here is my issue, when I try to get the price of a graphics card it gives me the wrong information. A card that should cost 379.99 on the website says in the terminal that the price is only 79.99. All of the prices are the wrong numbers in the terminal. How can I fix this?

Here is the code:

from bs4.element import PageElement
from bs4 import BeautifulSoup
from matplotlib.colors import LightSource
import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
print("loading website")
webpage_response = requests.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")


print("done loading website")
webpage = webpage_response.content
soup = BeautifulSoup(webpage, "html.parser")
#print(soup)
PageContent = soup.find(attrs={"id": "topPagination"})
MarginLeft = PageContent.find(attrs = {"class": "pages inline"})
CurrentPage = MarginLeft.find(attrs = {"class": "current"})


#helps get graphics card data
Content = soup.find("article", {"id": "productGrid"})
ULinContent = Content.find("ul")
Pwrapper = ULinContent.find_all(attrs={"class": "product_wrapper"})
#print(Pwrapper)
#print(Content)
#print(ULinContent)

CurrentPage = CurrentPage.get_text()
print("We are on page " + CurrentPage)
#print(MarginLeft)
#find number of web pages, then add all the way 
#up to that so we can get all of data
#NumberOfPages = PageContent.find(attrs={"class": "btn"})
#print(NumberOfPages)


#get prices of all rtx cards
prices = []
names = []

links = []
for link in soup.find_all("a"):
    links.append(link.get("href"))
print(links)

def GetCardDetails(prices, Pwrapper, names):
    for Pwrapp in Pwrapper:
        CheckBox = Pwrapp.find(attrs={"class": "checkbox"})
        CheckBoxText = CheckBox.get_text()
        
        #clean card name, the 15 and -11 index cleans compare from html
        CheckBoxClean = CheckBoxText[15:-11]
        #print(CheckBoxClean)
        names.append(CheckBoxClean)
        
        #price
        Card_Price = Pwrapp.find_all(attrs={"itemprop": "price"})
        print(Card_Price)
        print(Pwrapp.get_text())
        for price in Card_Price:
            GetText = price.get_text()
            prices.append(GetText)
            print(GetText)


    DicPrName = {"Graphics_Card_Name": names, "Prices": prices}
    #MAKE GRAPH
    data_frame2 = pd.DataFrame.from_dict(DicPrName)
    print(
        )
    plt.hist(prices)
    plt.show()
    print(DicPrName)

GetCardDetails(prices, Pwrapper, names)
#print(prices)
#print(names)

#average and graph them, get their data









#find the variabels to store using dict and table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2025-01-29 12:08:35

您可以使用CSS Attribute =值选择器如下:

from bs4 import BeautifulSoup
import requests

print("loading website")
webpage_response = requests.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter",
                                headers = {'User-Agent':'Mozilla/5.0'})


print("done loading website")
webpage = webpage_response.content
soup = BeautifulSoup(webpage, "html.parser")
print({i['data-name']:i['data-price'] for i in soup.select('[data-price]')})

You can use css attribute = value selectors as follows:

from bs4 import BeautifulSoup
import requests

print("loading website")
webpage_response = requests.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter",
                                headers = {'User-Agent':'Mozilla/5.0'})


print("done loading website")
webpage = webpage_response.content
soup = BeautifulSoup(webpage, "html.parser")
print({i['data-name']:i['data-price'] for i in soup.select('[data-price]')})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文