urlの文字列を作ろうとして試行錯誤しています.
python
1for k in range(9): 2 title2 = "https://dictionary.goo.ne.jp/srch/dialect/" + i + "/m9p" + str(k) + "u/"
とすると
TypeError: 'list' object is not callable
とエラーが出るのがなぜかわからないので教えてください.
具体的にはqiitaを参考に
python
1for j in ['1','2','3','4','5','6','7','8','9']: 2 title = "https://dictionary.goo.ne.jp/srch/dialect/" + i + "/m9p" + j + "u/"
は通るし
python
1for k in range(9): 2 title2 = "https://dictionary.goo.ne.jp/srch/dialect/" + i + "/m9p%du/" % k
は通りますがrangeを回してstr(j)して+で文字列連結するとうまくいきません.
python
1#タイトルを引っ張てくる 2import requests,bs4 3import codecs 4import os 5import glob 6 7# トップページ 8res = requests.get('https://dictionary.goo.ne.jp/dialect/') 9res.raise_for_status() # 例外発生 10soup = bs4.BeautifulSoup(res.text, "html.parser") # HTMLを取ってくる 11elems = soup.find_all("ul",class_="list-simple-d") # 指定して取り出して来た文字列 12box = [] # ul内のもの全ての配列 13pref = [] # 都道府県名の配列 14 15for elem in elems: 16 prefname = elem.getText() 17 str = prefname.replace("¥n"," ").split() # 改行をスペースに変換した文字列 18 for w in str: 19 box.append(w) #boxにwを追加 20 if len(w) < 5: 21 pref.append(w) #prefに都道府県名を追加 22 # print(pref) 23 # print(w) 24 25for i in pref: 26 for j in ['1','2','3','4','5','6','7','8','9']: 27 title = "https://dictionary.goo.ne.jp/srch/dialect/" + i + "/m9p" + j + "u/" 28 # print(type(j)) #->str 29 # print(title) 30 for k in range(9): 31 # print(k) 32 # num = str(k) 33 # title2 = "https://dictionary.goo.ne.jp/srch/dialect/" + i + "/m9p" + str(k) + "u/" #->できない! 34 # title2 = "https://dictionary.goo.ne.jp/srch/dialect/%s/m9p%du/" % i,k 35 title2 = "https://dictionary.goo.ne.jp/srch/dialect/" + i + "/m9p%du/" % k 36 print(title2) 37 38 39print(type(range(9))) #-> range 40 # preffilename = w + '.txt' 41 # file = open(preffilename,'w') 42 # elems = soup.find_all("dt", class_="title search-ttl-a") 43 # for elem in elems: 44 # print(elem.getText()) 45 # f.write(elem.getText()) 46 # 47 # os.chdir("/test_dir/dir_A") 48 # preffilelist = glob.glob("./*") 49 # print(preffilelist) 50 # f.close 51 52 53# print(pref) 54# print(len(pref)) 55
回答2件
あなたの回答
tips
プレビュー