python2とpython3を使えるようにしよう

[vagrant@localhost ~]$ python -V
Python 3.5.2
[vagrant@localhost centos6]$ python
Python 3.5.2 (default, Jul 28 2018, 11:25:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
[vagrant@localhost centos6]$ python3
Python 3.5.2 (default, Jul 28 2018, 11:25:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>

時間かかった~ 2日くらい

import urllib.request

url = "http://hogehoge/img/hoge.png"
savename = "test.png"

urllib.request.urlretrieve(url, savename)
print("保存しました")

[vagrant@localhost python]$ python3 app.py
保存しました

おおおお、
では、ヤフオクのmacbookを取得します。

import urllib.request

url = "https://wing-auctions.c.yimg.jp/sim?furl=auctions.c.yimg.jp/images.auctions.yahoo.co.jp/image/dr000/auc0407/users/6ba85c7e48fb6a8607eca71d0b7b7d6113140ce8/i-img1100x983-1532495826yzd18p69701.jpg"
savename = "mac.png"

urllib.request.urlretrieve(url, savename)
print("mac book")

[vagrant@localhost python]$ python3 app.py
mac book

OK

うん、ヤフオクだと、商品画像のアルゴリズムがよくわからんな。
img1100x983が画像サイズでしょうね。
6ba85c7e48fb6a8607eca71d0b7b7d6113140ce8
i-img1100x983-1532495826yzd18p69701.jpg

.bashrc

bashrcとは

bashは「シェルの種類のひとつで、shをパワーアップしたシェル」。
(ログインした後に画面上から)bashを起動したときに読み込まれる設定ファイル
シェルはsh、bash、csh、ksh、tcsh、zshなど

vi ~/.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions

ログイン時に「.bashrc」は読み込まれません。
ログイン時には「.bash_profile」というファイルが読み込まれます。

 .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
source ~/.bashrc

source ~/.bashrcとすると、.bashrcを読み込む。もちろん、source ~/.bash_profileで、bash_profileを読み込む

痛恨のミスから回復(Excel VBA)

1.フォームを立ち上げる

2.フォームに入力して、登録ボタンを押すと、、、
Excelに反映!

なんやねん、いけるやんけ。onClickでフォームの値はnullにする。

Private Sub CommandButton1_Click()
 With Worksheets("Sheet1")
  lastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  .Cells(lastRow, 1).Value = title.Text
  .Cells(lastRow, 4).Value = description.Text
  .Cells(lastRow, 5).Value = keyword.Text
  .Cells(lastRow, 7).Value = image.Text
 End With
 title.Text = ""
 description.Text = ""
 keyword.Text = ""
 image.Text = ""
End Sub

痛恨のミス

しまったー、VBA勉強するの忘れたー
VBAというか、ユーザーフォームね。
おいおいおい、困ったぞー、徹夜はきついぞー

formのtitleはCaptionで変えられる。今回は「突貫工事」にした。

ExcelのカラムをTitle, description, keyword, Imageとし、UserForm1も同様に、Title, description, keyword, Imageをつくる。

command buttonを押したとき

Private Sub CommandButton1_Click()
 With Worksheet("Sheet1")
  lastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  .Cells(lastRow, 1).Value = title.Text
  .Cells(lastRow, 2).Value = description.Text
  .Cells(lastRow, 3).Value = Keywords.Text
  .Cells(lastRow, 4).Value = image.Text
 End With
End Sub

no module named requests

[vagrant@localhost python]$ pip list
Package Version
————– ———
beautifulsoup4 4.6.0
certifi 2018.4.16
chardet 3.0.4
idna 2.7
libxml2-python 2.9.7
pip 18.0
requests 2.19.1
setuptools 38.5.2
urllib3 1.23
wheel 0.30.0
[vagrant@localhost python]$ python app.py
Traceback (most recent call last):
File “app.py”, line 3, in
import urllib3.requests
ImportError: No module named requests

# -*- coding: utf-8 -*-

import urllib3.requests

url = "http://uta.pw/shodou/img/28/214.png"
savename="test.png"

urllib3.requests.urlretrieve(url, savename)
print("保存しました。")

あれ?

業界に特化した検索エンジンをつくろう

googleの検索エンジン
-> crawlerが巡回
-> DOMを取得して配列に保存する

自作検索エンジン
-> crawlerが特定サイトを巡回
-> テキストを取得して、mongoDBに保存?

どういうアルゴリズムでリスト表示するか?
->最新のデータ
->クリック数

検索フィールド
->bodyに検索文字を含んでいた場合?

→データセットをmongoDBから取り出して、それをソートし直すのはいささか効率的でないような気がする

– タイトル、テキスト、リンク先を表示する。

そういえば、Googleにソートってないですね。
->レコメンドをどう出すか?

とりあえず、mongodbに入れるところから、始めよう。pug, sassも使いたい。
[vagrant@localhost freelance]$ mongo
MongoDB shell version: 3.2.20
connecting to: test
Server has startup warnings:
2018-07-21T18:36:29.147+0900 I CONTROL [initandlisten]
2018-07-21T18:36:29.148+0900 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
> show dbs
app 0.078GB
local 0.078GB
mydb 0.078GB

SimpleHTTPServerでサーバーは立ち上がるが

app.py

import SimpleHTTPServer
SimpleHTTPServer.test()

サーバーを起動します。
[vagrant@localhost python]$ python app.py
Serving HTTP on 0.0.0.0 port 8000 …
192.168.33.1 – – [22/Jul/2018 15:47:18] “GET /test.py HTTP/1.1” 200 –

htmlファイルは表示されます。

test.py

# -*- coding: utf-8 -*-

print 'Content-type: text/html\n'
print """
<!DOCTYPE html>
<html>
<head><meta charset="utf8"><title>CGIスクリプト</title></head>
<body>
これはサーバの実行結果として生成されたHTMLです<br>
今日はです
</body></html>
"""

何故?文字化け?

192.168.33.1 – – [22/Jul/2018 16:58:46] “GET /cgi-bin/ HTTP/1.1” 403 –
192.168.33.1 – – [22/Jul/2018 17:00:00] “GET /cgi-bin/sample.py HTTP/1.1” 200 –
: そのようなファイルやディレクトリはありません
192.168.33.1 – – [22/Jul/2018 17:00:00] CGI script exit status 0x7f00
何故だ、こりゃわからんな。

日経平均株価をスクレイピング

import urllib2
from bs4 import BeautifulSoup

html = urllib2.urlopen("hogehoge")
soup = BeautifulSoup(html, "html.parser")
tag =soup.find("td", class_="hogehoge").string
print(tag.encode("utf-8"))

[vagrant@localhost python]$ python app.py
22,697.88

米ドル、ニューヨークダウ、上海も行きたい。
find_allで書くと、tracebackが出てくる。何故だ。

tag =soup.find_all("td", class_="hogehoge").string
print(len(tag))

[vagrant@localhost python]$ python app.py
Traceback (most recent call last):
File “app.py”, line 8, in
tag =soup.find_all(“td”, class_=”header_shisuu_atai1″).string
File “/home/linuxbrew/.linuxbrew/Cellar/python@2/2.7.14_4/lib/python2.7/site-packages/bs4/element.py”, line 1807, in __getattr__
“ResultSet object has no attribute ‘%s’. You’re probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?” % key
AttributeError: ResultSet object has no attribute ‘string’. You’re probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

soup.find_all(“a”)だと行けるので、find_all()の中がまずそうだ。

findとfind_allだと、書き方が違うのか。。。リストは行けるんだが。。
[

22,697.88

,

111.38

,

25,058.12

,

2,829.27

]

‘ascii’ codec can’t encode characters と表示されたとき

import urllib2
from bs4 import BeautifulSoup

html = urllib2.urlopen("https://www.monex.co.jp/")
soup = BeautifulSoup(html, "html.parser")
tag =soup.title.string
print(tag)

エンコードできないと表示された。
[vagrant@localhost python]$ python app.py
Traceback (most recent call last):
File “app.py”, line 9, in
print(tag)
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-6: ordinal not in range(128)

こうなったら、タグを正規表現でreplaceしようと試したが上手くいかず、イライラMaxで数時間就寝。
改めて、試したら

import urllib2
from bs4 import BeautifulSoup

html = urllib2.urlopen("https://www.monex.co.jp/")
soup = BeautifulSoup(html, "html.parser")
tag =soup.title.string
print(tag.encode("utf-8"))

なんだ、エンコードを指定するのね♪
[vagrant@localhost python]$ python app.py
マネックス証券 | ネット証券(株・アメリカ株・投資信託)

大体気分転換すると上手くいくね。