import requests
import json
from bs4 import BeautifulSoup
from textwrap import dedent
from cookie import headers
page = 'http://vk-sova.3w.kz/users/index?sort_key=lastlogin&t=d'
def parser_sova(headers, page):
session = requests.Session()
request = session.get(page, headers=headers)
if request.status_code == 200:
soup = BeautifulSoup(request.content, 'html.parser')
for users in soup.find_all('tr'):
date_time = users.select_one(':nth-child(4) span')
name = users.select_one(':nth-child(3) a')
ip = users.select_one(':nth-child(4) span')
first_ip = ip['title'] if ip is not None else None
print(dedent(f'''\
--------------------------------------------------
Full name : {name.string}
Date / Time entry: {date_time.contents}
IP from the last entry point: {first_ip}
--------------------------------------------------
'''))
else:
print('error')
if __name__ == '__main__':
parser_sova(headers, page)
# json_add()
date_time = users.select_one(':nth-child(4) span')
<span title="145.255.168.186">Сегодня в 15:42</span>
а мне нужно лишь "Сегодня в 15:42", как мне получить контент ? .contents .string не помогают возвращают:AttributeError: 'NoneType' object has no attribute 'contents'
try:
date_time = users.select_one(':nth-child(4) span').text
except:
pass # Или date_time = '' - в зависимости от ситуации