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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Beautiful爬取海词网词汇意思和短语

發布時間:2024/1/8 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Beautiful爬取海词网词汇意思和短语 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Beautiful爬取海詞網詞匯意思和短語

直接上代碼

# -*- encoding:utf-8 -*- import urllib.request # 導入urllib庫的request模塊 from bs4 import BeautifulSoup import lxml #文檔解析器 import os #os模塊就是對操作系統進行操作 import numpy as np #列表、字典、字符串等中計算元素重復的次數urls=[] titles=[] target=input("請輸入你要查詢的單詞") #爬取所有新聞的url和標題,存儲在urls和titles中,這里range(1)表示只爬取1頁。 for i in range(1):url='http://dict.cn//'+targetres = urllib.request.urlopen(url) #調用urlopen()從服務器獲取網頁響應(respone),其返回的響應是一個實例html = res.read().decode('utf-8') #調用返回響應示例中的read(),utf-8解碼,可以讀取htmlsoup = BeautifulSoup(html, 'lxml')result = soup.find_all('div',attrs={'class': ['section def','layout coll']}) download_soup = BeautifulSoup(str(result), 'lxml') ol = download_soup.find_all('ol',slider="2") lt=[] ss="" for oli in ol:lt.append(oli.stripped_strings) for i in lt:for j in i:ss+=" "ss+=j print(ss)ul=download_soup.find_all('ul') soup1 = BeautifulSoup(str(ul), 'lxml') aa=soup1.find_all('li') import bs4 sentences='' for i in aa:for j in i.descendants:if j.string and type(j)!=bs4.element.Tag:print(j)sentences+=j.string.strip()s='意思:'+ss+'\n'+"短語:"+sentences#定義txt存儲路徑。 picpath='./newws2/'#這里我用的是本程序路徑,也可改為c盤或d盤等路徑。 def txt(name, text): # 定義函數名if not os.path.exists(picpath): # 路徑不存在時創建一個os.makedirs(picpath)savepath = picpath + name + '.txt'file = open(savepath, 'a', encoding='utf-8')#因為一個網頁里有多個標簽p,所以用'a'添加模式file.write(text)# print(text)file.close txt(target,s)

輸入樣例:book
結果:

總結

以上是生活随笔為你收集整理的Beautiful爬取海词网词汇意思和短语的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。