涓嬮潰鏄竴涓ず渚嬩唬鐮侊細
python
import requests
import re
from bs4 import BeautifulSoup
import pymysql.cursors
# 瀹氫箟瑕佽闂殑URL
url = "http://example.com"
# 鍙戦丟ET璇锋眰鑾峰彇缃戦〉鍐呭
response = requests.get(url)
html_content = response.text
# 浣跨敤姝e垯琛ㄨ揪寮忔彁鍙栨墍闇淇℃伅锛堣繖閲屽彧浣滀负绀轰緥锛
pattern = r'<div class="title">(.*?)</div>'
titles = re.findall(pattern, html_content)
# 杩炴帴MySQL鏁版嵁搴
connection = pymysql.connect(host='localhost', user='root', password='password', db='database')
cursor = connection.cursor()
try:
# 閬嶅巻鎻愬彇鍒扮殑鏍囬鍒楄〃锛屽苟灏嗗叾鎻掑叆鏁版嵁搴
for title in titles:
sql = "INSERT INTO table (column1) VALUES ('%s');" % title
cursor.execute(sql)
# 鎻愪氦浜嬪姟
connection.commit()
except Exception as e:
print("Error:", str(e))
finally:
# 鍏抽棴鏁版嵁搴撹繛鎺
cursor.close()
connection.close()
浠ヤ笂灏辨槸python requests 鐖彇骞舵彁鍙栧唴瀹瑰啓鍏ysql鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒