zoukankan      html  css  js  c++  java
  • [代码]--python爬虫联系--爬取成语

    闲来无事,玩了个成语接龙,于是就想用python爬取下成语网站上的成语,直接上代码:

    #coding=utf-8
    
    import requests
    from bs4 import BeautifulSoup
    import sqlite3
    import uuid
    
    conn = sqlite3.connect("idiombase.db3")  #创建sqlite.db数据库
    print ("open database success")
    conn.execute("drop table IF EXISTS idiom")
    query = """create table IF NOT EXISTS idiom(
        id VARCHAR(50),
        word VARCHAR(50)
    );"""
    conn.execute(query)
    print ("Table created successfully")
    
    all_url = 'http://chengyu.t086.com/'
    
    
    #http请求头
    Hostreferer = {
        'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
        'Referer':'http://chengyu.t086.com/'
    }
    
    word=['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z']
    
    for w in word:
    
        for n in range(1,100):
    
            url=all_url+'list/'+w+'_'+str(n)+'.html'
           
            start_html = requests.get(url,headers = Hostreferer)
            if(start_html.status_code==404):
                break
            start_html.encoding='gb2312'
            soup = BeautifulSoup(start_html.text,"html.parser")
    
            listw = soup.find('div',class_='listw')
            
            lista = listw.find_all('a')
            for p in lista:
                print(p.text)
                ids=str(uuid.uuid1())
                query = "insert into idiom (id,word) values ('"+ids+"','"+p.text+"');"
                conn.execute(query)
                conn.commit()

    取到数据后,保存在里sqlite数据库中,以后想用的时候随便取

  • 相关阅读:
    chattr
    chage命令
    passwd命令
    usermod命令
    userdel命令
    useradd命令
    信息热词分析系统重构
    pandas数据清洗
    python 中mysql数据库的读写
    java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
  • 原文地址:https://www.cnblogs.com/girliswater/p/9599710.html
Copyright © 2011-2022 走看看