如何使用美丽的汤中选择该标签中的名称数据?
我是新手编码;我刚开始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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用CSS Attribute =值选择器如下:
You can use css attribute = value selectors as follows: