解決したいこと
ウェブスクレイピングをしたいと考えております。
例えば
https://www.aaa.***(***は1〜10まで変化するとします。)
というサイトがあるとします。
そこで、
https://www.aaa.1
上記のURLサイトの中には
表があり、
https://www.aaa.2
には、表が無いものとします。
URLの末尾変更させ1~10までのサイトを読み込み
tableのあるサイトのみを抜き出したいと思っております。
python
1from bs4 import BeautifulSoup 2import requests 3import pandas as pd 4from pandas import Series,DataFrame 5import time 6from itertools import product 7 8RN=[] 9data=range(1,11) 10for i in data: 11 time.sleep(3) 12 url = 'https://www.aaa.'+str(i) 13 result = requests.get(url) 14 c = result.content 15 soup = BeautifulSoup(c,'lxml') 16 summary = soup.find('div') 17 table = summary.find_all('table') if not 'table':#**(1)** 18 if summary is None: 19 continue 20 rows = table[0].find_all('tr')
(1)ここでtableがサイト中に無い時には、下記の処理を中止して次のステップに進むようにしたいです。
サイト内にtableが無い場合には下記のエラーが出てきます。
AttributeError: 'NoneType' object has no attribute 'find_all'
基本的な条件式の記述方法ですが、教えていただけますでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。