Как я могу извлечь дату из сайта, используя красивый суп? - PullRequest
0 голосов
/ 25 января 2020

Я пытаюсь извлечь дату из этой статьи, например: https://www.ynet.co.il/articles/0, 7340, L-5665851,00.html # autoplay

Как вы можете видеть, она появляется здесь: enter image description here

Но проблема в том, что я не знаю, как извлечь это, поскольку это чистый текст, а не атрибут типа datetime или чего-то еще, может кто-нибудь мне помочь?

1 Ответ

1 голос
/ 25 января 2020

Вы можете сделать это с помощью Beautifulsoup и json:

import json
from bs4 import BeautifulSoup as bs
import requests

url = "https://www.ynet.co.il/articles/0,7340,L-5665851,00.html"
resp = requests.get(url)

soup = bs(resp.text,'lxml') 
#soup receives the response and parses it

data = json.loads(soup.find('script', type='application/ld+json').text)
#the target is contained inside a script tag; soup now extracts the script and python converts it to text; the converted string is in json format; json.loads() loads it into a variable
print(data['datePublished']) # you can access the info in the variable using the key names (datePublished, in this case)

Или вы можете сделать это с помощью l xml:

import lxml.html
doc = lxml.html.fromstring(resp.text)

targets = doc.xpath("//script[@type='application/ld+json']")
data = json.loads(targets[0].text)
print(data['datePublished']) 

(в обоих случаях):

2020-01-25T12: 47: 27z

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...