FREE way to scrape Tiktok accounts!

I wrote a script that will scrape Tiktok accounts for you.

You need to be logged in.

Tiktok caps the number of search results at about 380, so this will return a max of 400 on each run.

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:

https://www.youtube.com/watch?v=15nki8Mysa4

Written instructions:

  1. Go to Tiktok (must be logged in)
  2. Search for something
  3. Go to the "Accounts" tab
  4. Open the browser console (right-click -> inspect -> console)
  5. Paste the script below and hit enter
  6. 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 lastHeight = document.body.scrollHeight,
      newHeight
    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

      newHeight = document.body.scrollHeight
      if (lastHeight === newHeight) {
        clearInterval(interval)
        console.log(
          'Reached the bottom: No more content to load or infinite scroll limit reached.',
        )
        resolve('Done Scrolling') // Resolve the promise
      } else {
        lastHeight = newHeight
      }
    }, 1000) // You might need to adjust this interval
  })
}

function getAccountsFromHtml() {
  // get all existing data-e2e="search-user-container" divs
  const all = []
  const els = document.querySelectorAll('[data-e2e="search-user-container"]')
  // find the first a tag and get the href
  els.forEach((el) => {
    const a = el.querySelector('a')
    const href = a.getAttribute('href')
    const uniqueId = el.querySelector(
      '[data-e2e="search-user-unique-id"]',
    )?.textContent
    const nickname = el
      .querySelector('[data-e2e="search-user-nickname"]')
      ?.textContent?.split('·')?.[0]
      ?.trim()
    const followerCount = el.querySelector(
      '[data-e2e="search-follow-count"]',
    )?.textContent
    const description = el.querySelector(
      '[data-e2e="search-user-desc"]',
    )?.textContent
    all.push({
      profileUrl: `https://www.tiktok.com${href}`,
      handle: uniqueId,
      nickname,
      followerCount: parseFollowerCount(followerCount),
      description,
      email: getEmailInSignature(description),
    })
  })
  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 accounts = getAccountsFromHtml()
  console.log('accounts', accounts)
  console.log(`🥳 Woohoo! You just scraped ${accounts.length} tiktok accounts!`)
  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 urlObj = new URL(window.location.href)
  const searchQuery = urlObj.searchParams.get('q')
  createCSV(accounts, `${searchQuery}-tiktok-accounts.csv`)
})()

Sign up for my email list

Sign Up