【发布时间】:2019-12-18 14:20:03
【问题描述】:
我正在尝试从 Twitter 中提取发布日期。我已经成功地提取了帖子的名称和文本,但日期对我来说是一个难题。
作为输入,我有一个这样的链接列表:
- https://twitter.com/BarackObama/status/1158764847800213507;
- https://twitter.com/Pravitelstvo_RF/status/1160613745208549377
- https://twitter.com/BarackObama/status/1157016227681918981
我正在使用按类别搜索,但我认为这是一个问题。有时它适用于某些链接,有时则不适用。我已经尝试过这些解决方案:
soup.find("span",class_="_timestamp js-short-timestamp js-relative-timestamp")
soup.find('a', {'class': 'tweet-timestamp'})
soup.select("a.tweet-timestamp")
但这些都不是每次都能奏效的。
我当前的代码是:
data = requests.get(url)
soup = BeautifulSoup(data.text, 'html.parser')
gdata = soup.find_all("script")
for item in gdata:
items2 = item.find('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'}, href=True)
if items2:
items21 = items2.get('href')
items22 = items2.get('title')
print(items21)
print(items22)
我需要有发布日期的输出。
【问题讨论】:
标签: python twitter beautifulsoup