Build a Python Crawler to Auto‑Collect TV Drama Download Links

This article describes how the author built a Python web crawler to automatically generate numeric URLs, fetch TV drama pages from the 天天美剧 site, extract ed2k download links using regular expressions, and save them into organized text files, streamlining the download process with Thunder.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Python Crawler to Auto‑Collect TV Drama Download Links

The author, a fan of American TV series, wanted to simplify downloading episodes from the "天天美剧" site and wrote a Python web crawler to collect all drama links and save them to a text file for easy use with Thunder.

Initially the plan was to crawl the whole site from the homepage, but irregular URLs and many duplicates made it difficult, so the author switched to generating URLs by iterating over numeric IDs (e.g., http://cn163.net/archives/24016/).

Requests is used to check each generated URL; URLs returning 404 are skipped, while others are processed to extract download links.

def get_urls(self):
    try:
        for i in range(2015,25000):
            base_url='http://cn163.net/archives/'
            url=base_url+str(i)+'/'
            if requests.get(url).status_code == 404:
                continue
            else:
                self.save_links(url)
    except Exception as e:
        pass

The extraction relies on regular expressions to find ed2k links and episode titles, storing them in a dictionary keyed by season and episode numbers, then writing them in order to a .txt file.

# -*- coding:utf-8 -*-
import requests
import re
import sys
import threading
import time
reload(sys)
sys.setdefaultencoding('utf-8')
class Archives(object):
    def save_links(self,url):
        try:
            data=requests.get(url,timeout=3)
            content=data.text
            link_pat='"(ed2k://|file|[^\"]+?\.(S\d+)(E\d+)[^\"]+?1024X\d{3}[^\"]+?)"'
            name_pat=re.compile(r'<h2 class="entry_title">(.*?)</h2>',re.S)
            links = set(re.findall(link_pat,content))
            name=re.findall(name_pat,content)
            links_dict = {}
            count=len(links)
        except Exception as e:
            pass
        for i in links:
            links_dict[int(i[1][1:3])*100 + int(i[2][1:3])] = i
        try:
            with open(name[0].replace('/',' ') + '.txt','w') as f:
                print name[0]
                for i in sorted(list(links_dict.keys())):
                    f.write(links_dict[i][0] + '
')
            print "Get links ... ", name[0], count
        except Exception as e:
            pass
    def get_urls(self):
        try:
            for i in range(2015,25000):
                base_url='http://cn163.net/archives/'
                url=base_url+str(i)+'/'
                if requests.get(url).status_code == 404:
                    continue
                else:
                    self.save_links(url)
        except Exception as e:
            pass
    def main(self):
        thread1=threading.Thread(target=self.get_urls())
        thread1.start()
        thread1.join()
if __name__ == '__main__':
    start=time.time()
    a=Archives()
    a.main()
    end=time.time()
    print end-start

The full script runs in under 20 minutes for about 20,000 potential pages, skipping invalid URLs and handling filename restrictions (no slashes or special characters).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data collectionmultithreadingregexrequestsCrawlerweb-scraping
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.