Merhaba Arkadaşlar,
Bu makalemde sizlere python ile sitelerden description, keywords ve title bilgilerini çekip bunu MySQL veri tabanına göndermeniz için gerekli kodları aşağıda vereceğim.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import urllib import re import requests import MySQLdb import os import argparse from bs4 import BeautifulSoup from array import * parser = argparse.ArgumentParser() parser.add_argument("--url","-u", help="URL Adresi Yaziniz (http://example.com)") args = parser.parse_args() url = args.url r = requests.get(url) data = r.text soup = BeautifulSoup(data) description = soup.find_all('meta',attrs={'name':'description'}) keywords = soup.find_all('meta',attrs={'name':'keywords'}) title = soup.find_all('title') for desc in description: for key in keywords: for tt in title: db = MySQLdb.connect(host="localhost",user="root", passwd="root", db="root") db.set_character_set('utf8') insert_stmt = ( "INSERT INTO icerik (url, description, keywords, title) " "VALUES (%s, %s, %s, %s)" ) data=(url,desc,key,tt) cur = db.cursor() cur.execute(insert_stmt, data) db.commit() |
Sadece sistem üzerinde kullanacaksanız python vericek.py -u http://ibrahimerdogan.com.tr (vericek.py dosya adı farklı ad ile kaydederseniz o dosya adını yazınız ve websitesi örnek olarak kendi websitemi yazdım farklı linkler ve siteleri yaza bilirsiniz. (Not: http:// yada https:// siz veri çekemezseniz hata alırsınız)).
PHP ile işlem yapmak istiyorsanız php dosyanızında python’u çalıştırmak için aşağıdaki kodu kullanmanız gerekmektedir.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $url = "http://ibrahimerdogan.com.tr"; $command = "python vericek.py -u $url"; $pid = popen($command, "r"); while (!feof($pid)) { echo fread($pid, 256); flush(); ob_flush(); usleep(100000); } pclose($pid); ?> |
Ayrıca github üzerindende ulaşa bilirsiniz.
https://github.com/ibrahimerdogan/Python-ile-Siteden-Veri-ekme/blob/master/vericek.py
Bir başka makalede görüşmek dileği ile..