日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

[代码]--python爬虫联系--爬取成语

發(fā)布時間:2023/12/13 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 [代码]--python爬虫联系--爬取成语 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

閑來無事,玩了個成語接龍,于是就想用python爬取下成語網(wǎng)站上的成語,直接上代碼:

#coding=utf-8

import requests
from bs4 import BeautifulSoup
import sqlite3
import uuid

conn = sqlite3.connect("idiombase.db3")  #創(chuàng)建sqlite.db數(shù)據(jù)庫
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()

取到數(shù)據(jù)后,保存在里sqlite數(shù)據(jù)庫中,以后想用的時候隨便取

總結(jié)

以上是生活随笔為你收集整理的[代码]--python爬虫联系--爬取成语的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。