FREE way to scrape Tiktok videos from a hashtag!

I wrote a script that will scrape Tiktok videos from a hashtag for you.

You need to be logged in.

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

https://www.youtube.com/watch?v=kbMvRkDMVuc

Tiktok caps the number of search results at about 400, 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:

Written instructions:

  1. Go to tiktok.com/tag/your-hashtag, tiktok.com/tag/superbowl for example
  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 lastHeight = document.body.scrollHeight,
      newHeight
    const interval = setInterval(async () => {
      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/',
      )
      window.scrollTo(0, document.body.scrollHeight)
      await new Promise((r) => setTimeout(r, 3000))

      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
      }
    }, 3000) // You might need to adjust this interval
  })
}

function getDeepestLastChild(element) {
  if (element.lastElementChild) {
    // If there's a last child, go deeper
    return getDeepestLastChild(element.lastElementChild)
  } else {
    // If there are no more child elements, return the current element
    return element
  }
}

function getVideosFromHtml() {
  const all = []
  const els = document.querySelectorAll(
    '[data-e2e="challenge-item-list"] > div',
  )
  // find the first a tag and get the href
  els.forEach((el) => {
    const videoItem = el.querySelector('[data-e2e="challenge-item"]')
    const postUrl = videoItem.querySelector('a').getAttribute('href')
    const coverSrc = videoItem.querySelector('img').getAttribute('src')
    const uniqueId = videoItem.querySelector(
      '[data-e2e="challenge-item-username"]',
    )?.textContent

    const description = el.querySelector(
      '[data-e2e="challenge-item-desc"]',
    )?.textContent

    all.push({
      postUrl,
      profileUrl: `https://www.tiktok.com/@${uniqueId}`,
      handle: uniqueId,
      description,
    })
  })
  return all
}

;(async function () {
  await autoScroll()
  const videos = getVideosFromHtml()
  console.log('videos', videos)
  console.log(`🥳 Woohoo! You just scraped ${videos.length} tiktok videos!`)
  console.log('If you need Tiktok leads, check out https://tokfinder.co/')
  console.log(
    '100k+ TikTok accounts (with emails) for just $97: https://tokfinder.co/',
  )
  const hashtag = window.location.pathname.split('/')[2]
  createCSV(videos, `${hashtag}-tiktok-videos.csv`)
})()

Sign up for my email list

Sign Up