• Skip to main content
  • Skip to primary sidebar

学習記録

河副 太智

DBデータ格納先ドライブの変更

2020年3月28日 by 河副 太智 Leave a Comment

PostgreSQLサービスを停止する

C:\Program Files\PostgreSQL\9.0\data をそのまままるっとコピーする。
(D:\postgresql\data にコピーしたとする)

コピーした先のフォルダに対してpostgresユーザにフルコントロールアクセス権を付与する

レジストリエディタで「HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\postgresql-9.0\ImagePath」の -D の引数を変更する(-D “D:/postgresql/data”)

Windows再起動(しなくてもいいかもしれない)
PostgreSQLサービスを起動する
shima111の日記様から引用

windows10の場合はタスクバーに「regedit」と入力すればレジストリエディタが開く

Filed Under: PosgreSQL

PostgreSQL要素検索

2020年3月27日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import psycopg2
 
# Open a DB session
dbSession = psycopg2.connect("dbname='socialmedia' user='postgres' password='aa'")
dbSession.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
# Open a database cursor
dbCursor = dbSession.cursor();
 
 
dbCursor.execute("select * from test_table where cityname = 'New York City'")
 
serch_keyword = dbCursor.fetchone()
print(serch_keyword)
 
dbSession.close()  
dbCursor.close()

返り値は以下になる

(1, ‘New York City’, Decimal(‘40.73’), Decimal(‘-73.93’))

Filed Under: PosgreSQL

Jupyterからテーブル作成して要素追加

2020年3月25日 by 河副 太智 Leave a Comment

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
import psycopg2
 
# Open a DB session
dbSession = psycopg2.connect("dbname='socialmedia' user='postgres' password='abc'");
dbSession.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT);
# Open a database cursor
dbCursor = dbSession.cursor();
# SQL statement to create a table
sqlCreateTable  = "CREATE TABLE test_table(id bigint, cityname varchar(128), latitude numeric, longitude numeric);";
# Execute CREATE TABLE command
dbCursor.execute(sqlCreateTable);
# Insert statements
sqlInsertRow1  = "INSERT INTO test_table values(1, 'New York City', 40.73, -73.93)";
sqlInsertRow2  = "INSERT INTO test_table values(2, 'San Francisco', 37.733, -122.446)";
 
 
# Insert statement
dbCursor.execute(sqlInsertRow1);
dbCursor.execute(sqlInsertRow2);
 
# Select statement
sqlSelect = "select * from test_table";
dbCursor.execute(sqlSelect);
rows = dbCursor.fetchall();
 
# Print rows
for row in rows:
    print(row);

結果

(1, ‘New York City’, Decimal(‘40.73’), Decimal(‘-73.93’))
(2, ‘San Francisco’, Decimal(‘37.733’), Decimal(‘-122.446’))

Filed Under: Python 基本

Jupyterからデータベース新規作成

2020年3月25日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
 
# Connect to PostgreSQL DBMS
 
con = psycopg2.connect("user=postgres password='abc'");
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT);
 
# Obtain a DB Cursor
cursor          = con.cursor();
name_Database   = "socialmedia";
 
# Create table statement
 
sqlCreateDatabase = "create database "+name_Database+";"
 
# Create a table in PostgreSQL database
cursor.execute(sqlCreateDatabase);

 

Filed Under: PosgreSQL

タイムアウトでリトライ

2020年3月18日 by 河副 太智 Leave a Comment

タイムアウトでリトライ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
i = 0
while i < 20:
    try:
        driver.get("https://unipass.customs.go.kr/clip/index.do")
        print("成功")
 
    except TimeoutException:
        i = i + 1
        print("Timeout, Retrying... (%(i)s/%(max)s)")
        continue
 
    else:
        i = i + 1
        print("その他, Retrying...")
        continue

 

Filed Under: python3

TOR proxyを利用してseleniumスクレイピング

2020年3月18日 by 河副 太智 Leave a Comment

proxy経由でスクレイピングをする際はまずTORブラウザーをダウンロードする
ダウンロード

TORブラウザを起動させて右上の設定ボタンから
Option→Tor

Advancedの設定

SOCKS 5 → 127.0.0.1 → 9150と入力

TORブラウザを起動させたままプログラムを実行

1
2
3
4
5
6
7
8
9
10
11
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--proxy-server=socks5://127.0.0.1:9150")
driver = webdriver.Chrome(options=chrome_options)
# driver = webdriver.Chrome(executable_path='ChromeDriverPath', chrome_options=options)
driver.get("http://icanhazip.com/")
print(driver.page_source)
driver.close()

上記のコードで自分のPCと異なるIPアドレスが出てくれば準備OK
例:<html xmlns=”http://www.w3.org/1999/xhtml”><head></head><body><pre style=”word-wrap: break-word; white-space: pre-wrap;”>45.14.148.96</pre></body></html>

 

↓googleをスクレイピング

1
2
3
4
5
6
7
8
9
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
 
chrome_options = Options()
chrome_options.add_argument("--proxy-server=socks5://127.0.0.1:9150")
driver = webdriver.Chrome(options=chrome_options)
 
driver.get("http://google.com")
driver.close()

↓タイムアウトに対応

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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
 
chrome_options = Options()
chrome_options.add_argument("--proxy-server=socks5://127.0.0.1:9150")
driver = webdriver.Chrome(options=chrome_options)
 
driver.get("http://google.com")
time.sleep(2)
 
 
i = 0
 
while i < 100:
    try:
        time.sleep(10)
        search_box = driver.find_element_by_id("TOPMENU_LNK_M_ULS0200000000")
        print("見つかったのでブレイク")
        break
        
    except:
        i = i + 1
        print("再度トライ" + str(i) + "回目")
        driver.get("http://google.com")
        time.sleep(2)
 
        continue
        
driver.close()

 

Filed Under: スクレイピング

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 6
  • Page 7
  • Page 8
  • Page 9
  • Page 10
  • Interim pages omitted …
  • Page 66
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in