0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go To Eat 北海道キャンペーンの取扱店リストのPDFをCSVに変換

Last updated at Posted at 2020-11-10

Go To Eat 北海道キャンペーンの取扱店リストのPDFをCSVに変換

文字が消える

  • tabula・pdfbox・popplerだと「越漢熊煮新騒鶴」の文字が消えてしまう

  • 道北の「熊子ラーメン東旭川店」を検索してもヒットしない「子ラーメン東旭川店」だとヒット

  • pdfminerを使っているcamelot・pdfplumberは「(cid:1279)」という形で書き出されるためPDFの表示を確認して置換ができる

  • 作成ソフトは「cubepdf」、フォントは游ゴシック

cubepdf.png

font.png

shin.png

kuma.png

プログラム

import camelot

import requests
from bs4 import BeautifulSoup

from urllib.parse import urljoin

import pandas as pd

url = "https://gotoeat-hokkaido.jp/general/particStores/"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
}

r = requests.get(url, headers=headers)
r.raise_for_status()

soup = BeautifulSoup(r.content, "html.parser")

dfs = []

for li in soup.select("ul.cf > li > a"):

    link = urljoin(url, li.get("href"))

    area = li.get_text(strip=True)

    tables = camelot.read_pdf(link, split_text=True, pages="all", strip_text="\n", )

    for table in tables:

        df_tmp = pd.DataFrame(table.data[1:], columns=table.data[0])
        df_tmp.columns = df_tmp.columns.map(lambda s: "".join(s.split()))
        df_tmp["地域"] = area

        dfs.append(df_tmp)

df

df = pd.concat(dfs)

df = df.fillna("").applymap(
    lambda s: s.replace("(cid:1279)", "")
    .replace("(cid:1535)", "")
    .replace("(cid:1791)", "")
    .replace("(cid:2303)", "")
    .replace("(cid:2559)", "")
    .replace("(cid:2815)", "")
    .replace("(cid:3071)", "")
)

# CJK部首/康熙部首を置換
tbl = str.maketrans(
    "⺃⺅⺉⺋⺎⺏⺐⺒⺓⺔⺖⺘⺙⺛⺟⺠⺡⺢⺣⺦⺨⺫⺬⺭⺱⺲⺹⺾⻁⻂⻃⻄⻍⻏⻑⻒⻖⻘⻟⻤⻨⻩⻫⻭⻯⻲⼀⼁⼂⼃⼄⼅⼆⼇⼈⼉⼊⼋⼌⼍⼎⼏⼐⼑⼒⼓⼔⼕⼖⼗⼘⼙⼚⼛⼜⼝⼞⼟⼠⼡⼢⼣⼤⼥⼦⼧⼨⼩⼪⼫⼬⼭⼮⼯⼰⼱⼲⼳⼴⼵⼶⼷⼸⼹⼺⼻⼼⼽⼾⼿⽀⽁⽂⽃⽄⽅⽆⽇⽈⽉⽊⽋⽌⽍⽎⽏⽐⽑⽒⽓⽔⽕⽖⽗⽘⽙⽚⽛⽜⽝⽞⽟⽠⽡⽢⽣⽤⽥⽦⽧⽨⽩⽪⽫⽬⽭⽮⽯⽰⽱⽲⽳⽴⽵⽶⽷⽸⽹⽺⽻⽼⽽⽾⽿⾀⾁⾂⾃⾄⾅⾆⾇⾈⾉⾊⾋⾌⾍⾎⾏⾐⾑⾒⾓⾔⾕⾖⾗⾘⾙⾚⾛⾜⾝⾞⾟⾠⾡⾢⾣⾤⾥⾦⾧⾨⾩⾪⾫⾬⾭⾮⾯⾰⾱⾲⾳⾴⾵⾶⾷⾸⾹⾺⾻⾼⾽⾾⾿⿀⿁⿂⿃⿄⿅⿆⿇⿈⿉⿊⿋⿌⿍⿎⿏⿐⿑⿒⿓⿔⿕戶黑",
    "乚亻刂㔾兀尣尢巳幺彑忄扌攵旡母民氵氺灬丬犭罒示礻罓罒耂艹虎衤覀西辶阝長镸阝青飠鬼麦黄斉歯竜亀一丨丶丿乙亅二亠人儿入八冂冖冫几凵刀力勹匕匚匸十卜卩厂厶又口囗土士夂夊夕大女子宀寸小尢尸屮山巛工己巾干幺广廴廾弋弓彐彡彳心戈戸手支攴文斗斤方无日曰月木欠止歹殳毋比毛氏气水火爪父爻爿片牙牛犬玄玉瓜瓦甘生用田疋疒癶白皮皿目矛矢石示禸禾穴立竹米糸缶网羊羽老而耒耳聿肉臣自至臼舌舛舟艮色艸虍虫血行衣襾見角言谷豆豕豸貝赤走足身車辛辰辵邑酉釆里金長門阜隶隹雨靑非面革韋韭音頁風飛食首香馬骨高髟鬥鬯鬲鬼魚鳥鹵鹿麥麻黃黍黒黹黽鼎鼓鼠鼻齊齒龍龜龠戸黒",
)

df = df.applymap(lambda s: s.translate(tbl))

df.reset_index(drop=True, inplace=True)

df.index += 1

df.to_csv("gotoeat_hokkaido.csv", encoding="utf_8_sig")
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?