[英]Unable to scrape some timestamp attached to a text from a webpage
我正在嘗試從網頁中抓取附加到文本的時間戳。 我可以完美地抓取文本,但無法找到時間戳。 不過,我可以從那里抓取附加到評論的其他時間戳。 帶有注釋的時間戳可以在腳本標簽中作為created_at
的值找到。 但是,我找不到我要找的那個。
我試過:
import re
import json
import requests
url = 'https://www.instagram.com/p/CEuX_8iH95S/'
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
r = s.get(url)
script_tag = json.loads(re.findall(r"window\._sharedData = (.*?});",r.text)[0])
post_content = script_tag['entry_data']['PostPage'][0]['graphql']['shortcode_media']['edge_media_to_caption']['edges'][0]['node']['text']
print(post_content)
如何解析附加到上述站點文本的時間戳?
您可以使用datetime
模塊中的.fromtimestamp()
方法解析時間戳。
這是如何做到的:
import datetime
import re
import json
import requests
url = 'https://www.instagram.com/p/CEuX_8iH95S/'
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
r = s.get(url)
script_tag = json.loads(re.findall(r'window\._sharedData = (.*?});', r.text)[0])
post_date = script_tag['entry_data']['PostPage'][0]['graphql']['shortcode_media']['taken_at_timestamp']
print(datetime.datetime.fromtimestamp(post_date).isoformat())
print(datetime.datetime.fromtimestamp(post_date).strftime("%b %d %Y %H:%M:%S"))
這打印:
2020-09-04T20:25:49
Sep 04 2020 20:25:49
如果您想了解有關日期格式的更多信息,請查看此處的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.