Python crawl data and write to database __ database

Source: Internet
Author: User
# CODING=UTF-8 Import JSON import requests from BS4 import beautifulsoup Import db # Get Data def get_data (URL): hea DERs = {' user-agent ': ' mozilla/5.0 (Windows NT 10.0;
    WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/63.0.3239.84 safari/537.36 '};
    Response = Requests.post (URL, headers=headers, verify=false);
    response.encoding = ' utf-8 ';
    Text = Response.text;
    # text = Text.replace (', ');
    # text = Text.replace (', ');
    Text = Text.replace (' \ n ', ');
    Text = Text.replace (' \ R ', ');


return text;
    # Data Write to File Def writedata (listdata): Db.dbinsert (listdata) # file = Open ("Jd.txt", "A +", encoding= ' utf-8 ');
    # for data in Listdata: # WriteData = json.dumps (data, ensure_ascii=false);
    # file.writelines (WriteData);
    # file.writelines (' \ n ');
    # File.flush ();


# file.close ();
    # Analysis of the desired data # Number of data formats returned list<object> def analyzedata (html): Listdata = [];
    Soup = beautifulsoup (html, ' lxml '); ContentList = Soup.select (' Div.p-name ');
        For content in contentlist:detail = {};
        detail[' key '] = Content.select (' a ') [0].attrs.get (' href ');
        detail[' value '] = Content.text.strip ();
    Listdata.append (detail);


return listdata;
page = 0;
Lastkey = ';
    While True: # paging Fetch Data page = page + 1;
    url = "";
    data = Get_data (URL);
    Listdata = analyzedata (data);
    Lastkeytemp = Listdata[len (listdata)-1].get (' key ');
    if lastkeytemp = = Lastkey:break;
    Else:lastkey = lastkeytemp;
 WriteData (Listdata);

Import PSYCOPG2


def dbinsert (listdata):
    conn = Psycopg2.connect (database= ", user=", password=
                            ',
                            port= ' 5432 ')
    cur = conn.cursor () for

    data in Listdata:
        try:
            Cur.execute (r "" INSERT into T_ Data ("Key", "value") VALUES (' "" "+ data[' key '] +" "" "," "" "+ data[
                ' value '] +" "" "" "")
        except PSYCOPG2. Error:
            continue

    Conn.commit ()
    # Print (' Insert successfully ')
    Conn.close ()

















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.