Python是一種強大的編程語言,非常適合用于爬取數據。在這篇文章中,我們將討論如何使用Python來爬取單詞,并將其存儲到數據庫中。
import requests from bs4 import BeautifulSoup import sqlite3 # 連接到數據庫 conn = sqlite3.connect('words.db') c = conn.cursor() # 創建表格 c.execute("CREATE TABLE IF NOT EXISTS words (word TEXT, definition TEXT)") # 從Merriam-Webster網站獲取單詞并解析definition def get_word_definition(word): url = 'https://www.merriam-webster.com/dictionary/{}'.format(word) response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') definition_elements = soup.findAll(class_='dtText') definition = "; ".join([x.text for x in definition_elements]) return definition # 爬取單詞并將其存儲到數據庫中 def crawl_words(): words = ['python', 'programming', 'database', 'web', 'html', 'css', 'javascript'] for word in words: definition = get_word_definition(word) c.execute("INSERT INTO words VALUES (?, ?)", (word, definition)) # 保存并關閉數據庫連接 conn.commit() conn.close() crawl_words()
上述代碼使用了requests和BeautifulSoup庫來獲取和解析單詞的definition,并使用sqlite3庫將單詞和definition存儲到數據庫中。我們可以通過添加更多單詞來爬取更多的單詞,并將其存儲到同一數據庫中。