Issue
Today I'm learning how to use BeautifulSoup on python, and at one point, I have to work with lists that are on one list. The problem is that I don't know how to apply .text or similar to the lists. Here's my code:
from typing import List
from bs4 import BeautifulSoup
import requests
import pandas as pd
from decimal import Decimal
ListaPreciosCromos = list()
ListaUrl = ['https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_495570&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2#p1_price_asc', 'https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_540190&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2#p1_price_asc', 'https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_607210&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2#p1_price_asc']
PageCromos = [requests.get(x) for x in ListaUrl]
SoupCromos = [BeautifulSoup(x.content, "html.parser") for x in PageCromos]
PrecioCromos = [x.find_all("span", {"data-price": True}) for x in SoupCromos]
for x in PrecioCromos:
PreciossinText = x.text
print(PreciossinText)
Shows the next error = AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
Solution
try this:
from typing import List
from bs4 import BeautifulSoup
import requests
import pandas as pd
from decimal import Decimal
ListaPreciosCromos = list()
ListaUrl = ['https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_495570&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2#p1_price_asc', 'https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_540190&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2#p1_price_asc', 'https://steamcommunity.com/market/search?category_753_Game%5B%5D=tag_app_607210&category_753_cardborder%5B%5D=tag_cardborder_0&category_753_item_class%5B%5D=tag_item_class_2#p1_price_asc']
PageCromos = [requests.get(x) for x in ListaUrl]
SoupCromos = [BeautifulSoup(x.content, "html.parser") for x in PageCromos]
PrecioCromos = [x.find_all("span", {"data-price": True}) for x in SoupCromos]
PreciossinText = [txt.text for x in PrecioCromos for txt in x]
print(PreciossinText)
output:
['$0.05 USD', '$0.05 USD', '$0.04 USD', '$0.05 USD', '$0.04 USD', '$0.05 USD', '$0.05 USD', '$0.07 USD', '$0.06 USD', '$0.07 USD', '$0.06 USD', '$0.06 USD', '$0.06 USD', '$0.05 USD', '$0.06 USD', '$0.05 USD', '$0.07 USD']
Answered By - user1740577
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.