Introduction to Python Crawler [11]: Semidimensional COS Graph Crawling

Semidimensional COS Graph Crawl-Write in front

Today, when I was browsing the website, an inexplicable link led me to jump to the half-dimensional website After opening it, I found that there was nothing interesting. Professional sensitivity reminded me of cosplay in an instant. This kind of website is bound to exist, so I am ready for me. Big crawler.

After opening the link above, I found out that my eighth feeling is good. Next is to find the entrance, you must find the entrance of the picture link before you can do the following operation

This page drags down and loads all the time. When you drag for a while, it stops. That's the time.

Discover the entrance, in my actual operation, in fact, also found many other entries, this is not explained one by one, get on the bus, enter the view more, found that the page is still a drop-down refresh layout, the professional term waterfall flow.

Semidimensional COS Graph Crawling-First Step of python Crawler

Open the developer's tools, switch to network, find many xhr requests, find this, on behalf of the site is easy to crawl.

Extract the links to be crawled and analyze the rules

Find that only one parameter is changing, and the change seems to have no rules to look for, no matter, look at the data, you can find the mystery of it.

The principle of this website is very simple, that is, by constantly getting the last since of each data and then getting the next data, then we can implement the code according to its rules, do not multi-threading, this rule is impossible to operate.
This time I store it in mongodb, because I can't get it all at once, so maybe I need to continue using it next time.

if __name__ == '__main__':
    ###  Some basic operations of mongodb   
    DATABASE_IP = ''
    DATABASE_PORT = 27017
    DATABASE_NAME = 'sun'
    start_url = "{}&grid_type=timeline&tag_id=399&sort=recent"
    client = MongoClient(DATABASE_IP, DATABASE_PORT)

    db = client.sun
    db.authenticate("dba", "dba")
    collection  =  db.bcy  # Prepare to insert data
Python Resource sharing qun 784758214 ,Installation packages are included. PDF,Learning videos, here is Python The gathering place of learners, zero foundation and advanced level are all welcomed.

Getting web data is a place where our previous experience has made it easy.

## Semidimensional COS Graph Crawling-Getting Data Function  
def get_data(start_url,collection):
    since = 0
    while 1:
            with requests.Session() as s:
                response = s.get(start_url.format(str(since)),headers=headers,timeout=3)
                res_data = response.json()
                if res_data["status"] == 1:
                    data = res_data["data"]  # Get the Data array
                ## data processing
                since = data[-1]["since"]  # since in the last json data to get 20 pieces of data
                ret = json_handle(data)   # The code implementation is as follows
                    collection.insert_many(ret)   # Batch Access Database
                    print("The above data was inserted successfully!!!!!!!!")
                except Exception as e:
                    print("Insertion failure")

        except Exception as e:
            print("!",end="Exceptions, please note")
            print(e,end=" ")
        print("Completion of the cycle")

Page parsing code

# Processing JSON data
def json_handle(data):
    # Extraction of key data
    list_infos = []
    for item in data:
        item = item["item_detail"]
            avatar = item["avatar"] # User Avatar
            item_id = item["item_id"] # Picture Details Page
            like_count = item["like_count"] # Number of likes
            pic_num = item["pic_num"] if "pic_num" in item else 0 # Total number of pictures
            reply_count =item["reply_count"]
            share_count =item["share_count"]
            uid = item["uid"]
            plain = item["plain"]
            uname = item["uname"]
        except Exception as e:
        return list_infos
Python Resource sharing qun 784758214 ,Installation packages are included. PDF,Learning videos, here is Python The gathering place of learners, zero foundation and advanced level are all welcomed.

Now it's done, and the code runs.

Tags: Python JSON MongoDB network

Posted on Mon, 22 Jul 2019 10:26:43 -0400 by Rabioza123