Home > Software design >  Problem with web-scraped text using Python
Problem with web-scraped text using Python

Time:11-01

I have issues getting text for the first printed item as nothing shows up except a blank line, but there are no issues with the second item text showing up in print. I would like to know if there is a workaround for this text print issue or if there is something about python coding that I have overlooked.

This code has been applied through Visual Studio 2022 using MS Edge web browser.

import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.betexplorer.com/tennis/atp-singles/tokyo/mcdonald-mackenzie-uchida-kaichi/UivPCtWD/")
webpage = response.content

soup = BeautifulSoup(webpage, "html.parser")

print(soup.select('li p')[0].text)

print(soup.select('li p')[1].text)

CodePudding user response:

Are you trying to get the date? Because if so, the code:

print(soup.select('li p')[1].text)

For me, I got the date with this line of code:

# We want to find the p element with this class
date = soup.find("p", {"class": "list-details__item__date"})["data-dt"]  
print(date)

EDIT: it also worked with your code, you just have to get the data-dt attribute

print(soup.select('li p')[0]["data-dt"])
  • Related