FREE way to scrape Tiktok comments from a video!

I wrote a script that will scrape Tiktok comments from a video for you.

You need to be logged in.

Here's a video on how to use the script:

https://www.youtube.com/watch?v=hi-d_b_d400

If you want to scrape other sites, check out my brand new AI scraper: ScrapeThat.ai

Here's a video on how to use the script:

Written instructions:

  1. Go to the video you want to scrape comments from
  2. Open the browser console (right-click -> inspect -> console)
  3. Paste the script below and hit enter
  4. Wait for the magic 🧙‍♂️
function createCSV(data, fileName) {
  const headers = Object.keys(data[0])

  const csvContent = [
    headers.join(','),
    ...data.map((row) =>
      headers
        .map((header) => {
          const value = row[header]
          if (value === null) return 'null'
          if (typeof value === 'string') {
            // Wrap all fields, including those without commas, in double quotes
            return `"${value.replace(/"/g, '""')}"`
          }
          return value
        })
        .join(','),
    ),
  ].join('\n')

  const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
  const link = document.createElement('a')

  if (navigator.msSaveBlob) {
    // IE 10+
    navigator.msSaveBlob(blob, fileName)
  } else {
    const url = URL.createObjectURL(blob)

    link.setAttribute('href', url)
    link.setAttribute('download', fileName || 'data.csv')
    document.body.appendChild(link)

    link.click()

    document.body.removeChild(link)
    URL.revokeObjectURL(url)
  }
}

function autoScroll() {
  return new Promise((resolve, reject) => {
    let lastCommentCount = getCommentsFromHtml().length,
      newCommentCount
    const interval = setInterval(async () => {
      window.scrollTo(0, document.body.scrollHeight)
      console.log(
        `Don't worry if you see errors. Give it a few minutes to run.`,
      )
      console.log(`Check out my site: https://thewebscrapingguy.com`)
      console.log(
        'If you need Tiktok leads, check out my product: https://tokfinder.co/',
      )
      console.log(
        '100k+ TikTok accounts (with emails) for just $97: https://tokfinder.co/',
      )
      console.log(
        'Or if you want to scrape many sites, check out my AI scraper: https://scrapethat.ai/',
      )
      await new Promise((r) => setTimeout(r, 2000)) // wait for 1 second

      newCommentCount = getCommentsFromHtml().length
      console.log('newCommentCount', newCommentCount)
      console.log('lastCommentCount', lastCommentCount)
      if (newCommentCount === lastCommentCount) {
        clearInterval(interval)
        console.log(
          'Reached the bottom: No more content to load or infinite scroll limit reached.',
        )
        resolve('Done Scrolling') // Resolve the promise
      } else {
        lastCommentCount = newCommentCount
      }
    }, 4000) // You might need to adjust this interval
  })
}

function getCommentsFromHtml() {
  // get all existing data-e2e="search-user-container" divs
  const all = []
  const els = document.querySelectorAll(
    '[data-e2e="search-comment-container"] > div >div',
  )
  // find the first a tag and get the href
  els.forEach((el) => {
    const comment = el.querySelector('[data-e2e="comment-level-1"]')?.innerText
    const name = el.querySelector('[data-e2e="comment-username-1"]')?.innerText
    const profileLink = el.querySelector('[data-e2e="comment-avatar-1"]')?.href
    const likes = el.querySelector('[data-e2e="comment-like-count"]')?.innerText
    all.push({
      comment,
      name,
      author_url: profileLink,
      likes: likes ? parseInt(likes) : 0,
    })
  })
  return all
}

function getEmailInSignature(signature) {
  if (!signature) return ''
  const emailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/g
  const emails = signature.match(emailRegex)
  return emails?.[0]
}

function parseFollowerCount(followerCount) {
  // followerCount is a string like "1.2M" or "1345" or "10.5K" anything more thank 1k is shortened
  const num = parseFloat(followerCount)
  if (followerCount.includes('M')) {
    return num * 1000000
  } else if (followerCount.includes('K')) {
    return num * 1000
  } else {
    return num
  }
}

;(async function () {
  await autoScroll()

  const comments = getCommentsFromHtml()
  console.log(comments)

  console.log(`🥳 Woohoo! You just scraped ${comments.length} tiktok comments!`)
  console.log('If you need more, check out https://tokfinder.co/')
  console.log(
    '100k+ TikTok accounts (with emails) for just $97: https://tokfinder.co/',
  )
  const videoId = window.location.pathname.split('/')[2]
  createCSV(comments, `${videoId}-tiktok-comments.csv`)
})()

Sign up for my email list

Sign Up