I am trying to scrape data from a website using scrapy. This is the css path
<div _ngcontent-amb-c25="" appcoloredmultiplier="" class="bubble-multiplier font-weight-bold" style="padding: 2px 11px; border-radius: 11px; color: rgb(52, 180, 255);"> 1.21x </div>
but I’d like to extract data between the tag which is 1.21x how do I update my code to extract the data I stated.
def parse(self, response):
# Extract game history data from the webpage
game_history_elements = response.css('div.bubble-multiplier')
# Extract the multiplier value from each game history element
game_history = [re.search(r'(d+.d+)x', element.css('::text').get()).group(1) for element in game_history_elements]
# Print the game history data
print(game_history)
2
Answers
try this
As mentioned in the comments, you can get the text between tags using the
::text
css directive in your xpath expression and then applying either theget
orgetall
methods on the selector.If there is more than one
div
with the classbubble-multiplier
, and you want the text for each of them then you would usegetall()
, on the other hand if there is only a single matching element or you simply only want the first, then you would usegetall()
.or
You can still use
getall
when there is only a single match, the only difference would be that the the return value would be alist
with just the one string as contents.