Package: comcrawl
comcrawlcomcrawlis a python package for easily querying and downloading pages fromcommoncrawl.org.IntroductionI was inspired to makecomcrawlby reading thisarticle.Note:I made this for personal projects and for fun. Thus this package is intended for use in small to medium projects, because it is not optimized for handling gigabytes or terrabytes of data. You might want to check outcdx-toolkitorcdx-index-clientin such cases.What is Common Crawl?The Common Crawl project is an"open repository of web crawl data that can be accessed and analyzed by anyone".
It contains billions of web pages and is often used for NLP projects to gather large amounts of text data.Common Crawl provides asearch index, which you can use to search for certain URLs in their crawled data.
Each search result contains a link and byte offset to a specific location in theirAWS S3 bucketsto download the page.What doescomcrawloffer?comcrawlsimplifies this process of searching and downloading from Common Crawl by offering a simple API interface you can use in your python program.Installationcomcrawlis available on PyPI.Install it via pip by running the following command from your terminal:pip install comcrawlUsageBasicThe HTML for each page will be available as a string in the 'html' key in each results dictionary after calling thedownloadmethod.fromcomcrawlimportIndexClientclient=IndexClient()client.search("reddit.com/r/MachineLearning/*")client.download()first_page_html=client.results[0]["html"]MultithreadingYou can leverage multithreading while searching or downloading by specifying the number of threads you want to use.Please keep in mind to not overdo this, so you don't put too much stress on the Common Crawl servers (have a look atCode of Conduct).fromcomcrawlimportIndexClientclient=IndexClient()client.search("reddit.com/r/MachineLearning/*",threads=4)client.download(threads=4)Removing duplicates & SavingYou can easily combine this package with thepandaslibrary, to filter out duplicate results and persist them to disk:fromcomcrawlimportIndexClientimportpandasaspdclient=IndexClient()client.search("reddit.com/r/MachineLearning/*")client.results=(pd.DataFrame(client.results).sort_values(by="timestamp").drop_duplicates("urlkey",keep="last").to_dict("records"))client.download()pd.DataFrame(client.results).to_csv("results.csv")The urlkey alone might not be sufficient here, so you might want to write a function to compute a custom id from the results' properties for the removal of duplicates.Searching subsets of IndexesBy default, when instantiated, theIndexClientfetches a list of currently available Common Crawl indexes to search. You can also restrict the search to certain Common Crawl Indexes, by specifying them as a list.fromcomcrawlimportIndexClientclient=IndexClient(["2019-51","2019-47"])client.search("reddit.com/r/MachineLearning/*")client.download()Logging HTTP requestsWhen debugging your code, you can enable logging of all HTTP requests that are made.fromcomcrawlimportIndexClientclient=IndexClient(verbose=True)client.search("reddit.com/r/MachineLearning/*")client.download()Code of ConductWhen accessing Common Crawl, please beware these guidelines posted by one of the Common Crawl maintainers:https://groups.google.com/forum/#!msg/common-crawl/3QmQjFA_3y4/vTbhGqIBBQAJ