Home>
I want to solve

I want to do web scraping.
For example
https://www.aaa.* (*changes from 1 to 10.)
Suppose you have a site called
So

https: //www.aaa.1
Some of the above URL sites are
There is a table,
https: //www.aaa.2
Suppose there is no table.

Change the end of the URL and load sites 1 to 10
I want to extract only sites with tables.

from bs4 import BeautifulSoup
import requests
import pandas as pd
from pandas import Series, DataFrame
import time
from itertools import product
RN = []
data = range (1,11)
for i in data:
    time.sleep (3)
    url = 'https: //www.aaa.'+str (i)
    result = requests.get (url)
    c = result.content
    soup = BeautifulSoup (c, 'lxml')
    summary = soup.find ('div')
    table = summary.find_all ('table') if not 'table': # ** (1) **
    if summary is None:
    continue
    rows = table [0] .find_all ('tr')


(1) If table is not in the site, I would like to stop the following processing and proceed to the next step.

If there is no table in the site, the following error will appear.
AttributeError:'NoneType'object has no attribute'find_all'

Can you tell me how to write a basic conditional expression?

  • Answer # 1

    I would like you to investigateformore specifically, but usecontinueto skip the iteration and go to the next step. The

    if len (table) == 0:
       continue

  • Answer # 2

    tablevariable is[]when there is no TABLE tag. Will you be?

    for i in Round:
        time.sleep (3)
        url = 'https: //www.aaa.'+str (i)
        result = requests.get (url)
        c = result.content
        soup = BeautifulSoup (c, 'lxml')
        summary = soup.find ('div')
        table = summary.find_all ('table')
        if not table:
            continue
        rows = table [0] .find_all ('tr')