title: Using sys.argv for webscraping, and trying run it through CMD console. I am new to this, adn dont knwo how to solve "NoneType" error. Using pycharm
body: <pre><code>import sys 
  import requests 
  import csv 
  import os 
  from bs4 import BeautifulSoup
  
  
  
  def main():
      url = input_arguments()[0]
      filename = input_arguments()[1]
      header = summary_info(url)
      data = data_scraping(url)
      save_to_csv(filename, header, data)
      check_file(filename)
  
  
  def url_list():
      url = &quot;https://volby.cz/pls/ps2017nss/ps3?xjazyk=CZ&quot;
      r = requests.get(url)
      soup = BeautifulSoup(r.text, &quot;html.parser&quot;)
      list_href = []
      for index in range(1, 15):
          find = soup.find_all((&quot;td&quot;, {&quot;headers&quot;: f&quot;{index}sa3&quot;}))
          for href in find:
              url_part = &quot;https://volby.cz/pls/ps2017nss/&quot;
              all_url = url_part + href.a[&quot;href&quot;]
              list_href.append(all_url)
      return list_href
  
  def input_arguments():
      &quot;&quot;&quot;
      Kontroluje a přijímá argumet URL a název výstupního souboru.
      &quot;&quot;&quot;
      if len(sys.argv) != 3:
          print(
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f&quot;| 1) Jako první argument zadej URL adresu v uvozovkách:                    |&quot;,
              f'|    např.: &quot;https://volby.cz/pls/ps2017&quot;                                  |',
              f&quot;| 2) Jako druhý argument zadej název souboru pro uložení dat v uvozovkách: |&quot;,
              f'|    např.: &quot;vysledky_lipno.csv&quot;                                          |',
              f&quot;| 3) Celkový vzor:                                                         |&quot;,
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f'| election_scraper.py &quot;https://volby.cz/pls/ps2017&quot; &quot;vysledky_lipno.csv&quot;  |',
              f&quot;+--------------------------------------------------------------------------+&quot;,
              sep=&quot;\n&quot;
              )
          quit()
      elif sys.argv[2] in url_list():
              print(
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f&quot;| Jako prní argument zadej URL adresu a jako druhý název souboru.          |&quot;,
              f&quot;| Argumenty zadej v uvozovkách a odděl mezerou.                            |&quot;,
              f&quot;| Viz vzor:                                                                |&quot;,
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f'| election_scraper.py &quot;https://volby.cz/pls/ps2017&quot; &quot;vysledky_lipno.csv&quot;  |',
              f&quot;+--------------------------------------------------------------------------+&quot;,
              sep=&quot;\n&quot;
              )
              quit()
      elif sys.argv[1] not in url_list():
              print(
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f&quot;| Tato URL adresa není podporována.                                        |&quot;,
              f&quot;| Zadej podporovanou URL adresu způsobem viz vzor:                         |&quot;,
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f'| election_scraper.py &quot;https://volby.cz/pls/ps2017&quot; &quot;vysledky_lipno.csv&quot;  |',
              f&quot;+--------------------------------------------------------------------------+&quot;,
              sep=&quot;\n&quot;
              )
              quit()
      elif not sys.argv[2].endswith('.csv'):
              print(
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f&quot;| Název souboru musí končit příponou .csv                                  |&quot;,
              f&quot;| Viz vzor:                                                                |&quot;,
              f&quot;+--------------------------------------------------------------------------+&quot;,
              f'| election_scraper.py &quot;https://volby.cz/pls/ps2017&quot; &quot;vysledky_lipno.csv&quot;  |',
              f&quot;+--------------------------------------------------------------------------+&quot;,
              sep=&quot;\n&quot;
              )
              quit()
      else:
          url = sys.argv[1]
          filename = sys.argv[2]
      return url, filename
  
  def summary_info(url):
      &quot;&quot;&quot;
      Vypíše názvy stran.
      &quot;&quot;&quot;
      header = [
          &quot;kód obce,&quot;
          &quot;název obce&quot;,
          &quot;voliči v seznamu&quot;,
          &quot;vydané obálky&quot;,
          &quot;platé hlasy&quot;,
      ]
  
      first_page = requests.get(url)
  
      print(
          f&quot;Stahuji informace z vybraného URL:&quot;,
          f&quot;{url}&quot;,
          sep=&quot;\n&quot;
      )
  
      soup = BeautifulSoup(first_page.text, &quot;html.parser&quot;)
      first_page_href = soup.find(&quot;td&quot;, {&quot;class&quot;: &quot;cislo&quot;}).a[&quot;href&quot;]
  
      part_url = &quot;https://volby.cz/pls/ps2017nss/&quot;
      header_url = part_url + first_page_href
  
      second_page = requests.get(header_url)
      soup = BeautifulSoup(second_page.text, &quot;html.parser&quot;)
  
      for name in (soup.find_all(&quot;td&quot;, {&quot;class&quot;: &quot;overflow_name&quot;})):
          header.append(name.text)
  
      return header
  
  def data_scraping(url):
      &quot;&quot;&quot;
      ( This function allows download data)Funkce bere kód obce a název obce a přidává data do listu ze stránky vybrané
      uživatelem. Následně přistupuje přes kód obce ke zbylým datům, která přidává do listu.
      Jakmile má funkce všechna data pro danou obec/řádek, tak přidává list do listu data.
      Tento postup se opakuje pro všechny obce.
      &quot;&quot;&quot;
  
      first_page = requests.get(url)
  
      print(
          f&quot;stahuji data z vybraného URL:&quot;
          f&quot;(url&quot;,
          sep=&quot;\n&quot;
          )
  
      soup = BeautifulSoup(first_page.text, &quot;html.parser&quot;)
  
      first_page_code = soup.find_all(&quot;td&quot;, {&quot;class&quot;: &quot;cislo&quot;})
      first_page_names = soup.find_all(&quot;td&quot;, {&quot;class&quot;: &quot;overflow_name&quot;})
  
      if len(first_page_names) == 0:
          first_page_names = soup.find_all(&quot;td&quot;, {&quot;headers&quot;: &quot;t1sa1 t1sb2&quot;})
  
      first_page_hrefs = [href.a[&quot;href&quot;] for href in first_page_code]
  
      data = []
  
      part_url = &quot;https://volby.cz/pls/ps2017nss/&quot;
  
      for index, result in enumerate(first_page_hrefs, 0):
          row_list = []
  
          second_url = part_url + result
  
          second_page = requests.get(second_url)
          soup = BeautifulSoup(second_page.text, &quot;html.parser&quot;)
  
          row_list.append(first_page_code[index].text)
          row_list.append(first_page_names[index].text)
  
          row_list.append((soup.find(&quot;td&quot;, {&quot;headers&quot;: &quot;sa2&quot;}).text).replace('\xa0', ''))
          row_list.append((soup.find(&quot;td&quot;, {&quot;headers&quot;: &quot;sa3&quot;}).text).replace('\xa0', ''))
          row_list.append((soup.find(&quot;td&quot;, {&quot;headers&quot;: &quot;sa6&quot;}).text).replace('\xa0', ''))
  
          first_candidate_parties = (soup.find_all(&quot;td&quot;, {&quot;headers&quot;: &quot;t1sa2 t1sb3&quot;}))
          for data_candidate in first_candidate_parties:
              row_list.append(data_candidate.text.replace('\xa0', ''))
  
          second_candidate_parties = (soup.find_all(&quot;td&quot;, {&quot;headers&quot;: &quot;t2sa2 t2sb3&quot;}))
          for data_candidate in second_candidate_parties:
              numeric = (data_candidate.text.replace('\xa0', ''))
              if numeric.isnumeric():
                  row_list.append(numeric)
  
          data.append(row_list)
  
      return data
  
  
  def check_file(filename):
      dash = &quot;-&quot; * len(filename)
  
      if filename in os.listdir():
          print(
              f&quot;+---------------------------{dash}+&quot;,
              f&quot;| Data uložena do souboru: {filename} |&quot;,
              f&quot;+---------------------------{dash}+&quot;,
              f&quot;Ukončuji program...&quot;,
              sep=&quot;\n&quot;, end=(&quot;&quot;)
          )
      else:
          print(
              f&quot;+--------------------{dash}+&quot;,
              f&quot;| Soubor nenalezen: {filename} |&quot;,
              f&quot;+--------------------{dash}+&quot;,
              f&quot;Ukončuji program...&quot;,
              sep=&quot;\n&quot;, end=(&quot;&quot;)
          )
  
  
  def save_to_csv(filename, header, data):
      &quot;&quot;&quot;
      Uloží data do csv.
      &quot;&quot;&quot;
      with open(filename, mode=&quot;w&quot;, newline=&quot;&quot;, encoding=&quot;utf-8&quot;) as data_csv:
          writer = csv.writer(data_csv)
          print(
              f&quot;Ukládám data do vybraného souboru:&quot;,
              f&quot;{filename}&quot;,
              sep=&quot;\n&quot;
          )
          writer.writerow(header)
          for row in data:
              writer.writerow(row)
  
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  <p>You must insert to Pycharm terminal 3 arguments for start program and it looks like this:</p>
  <ul>
  <li><p>py election_scraper.py</p>
  </li>
  <li><p>Url in quotation marks
  &quot;https://volby.cz/pls/ps2017nss/ps311?xjazyk=CZ&amp;xkraj=6&amp;xobec=566403&amp;xvyber=4204&quot;</p>
  </li>
  <li><p>Name of result file in csv format like &quot;vysledky_lipno.csv&quot;</p>
  </li>
  </ul>
  <p>But when I try do this, still getting error like this:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\Admin\PycharmProjects\pythonProject3ElectionScraper\election_scraper.py&quot;, line 226, in &lt;module&gt;      
      main()
    File &quot;C:\Users\Admin\PycharmProjects\pythonProject3ElectionScraper\election_scraper.py&quot;, line 16, in main
      url = input_arguments()[0]
    File &quot;C:\Users\Admin\PycharmProjects\pythonProject3ElectionScraper\election_scraper.py&quot;, line 55, in input_arguments
      elif sys.argv[2] in url_list():
    File &quot;C:\Users\Admin\PycharmProjects\pythonProject3ElectionScraper\election_scraper.py&quot;, line 33, in url_list       
      all_url = url_part + href.a[&quot;href&quot;]
  TypeError: 'NoneType' object is not subscriptable
  </code></pre>
  <p>I am new to this, so sorry if this ASK is stupid, but i am trying handle this for so long, and I decided to ask here for help. Thank you.</p>