-3

I'm trying to write a python script to get the number of url used in a particuler web page:

TypeErrorTraceback (most recent call last)
<ipython-input-7-a3136853c4b2> in <module>()
 30     return no_use
 31 
 32 print(mining_webpage())
TypeError: mining_webpage() missing 2 required positional arguments: 'url' and 'list'

This is the code:

from bs4 import BeautifulSoup as bs 
import requests
import re
import pandas as pd
import matplotlib as plt

def mining_webpage(url,list):
   '''Finds the howmany websites are used in the webpage and counts its total number'''
    reallink=[]
    tokens=[]
    list1=[]
    no_use={}
    link=url
    word_list=list
    text=requests.get(link).text
    soup=bs(text)
    for l in soup.find_all(href=re.compile('https')):
        reallink.append(l.get('href').split('//'))
    for lists in reallink:
        '''print(lists[-1])'''
        list1.append(lists[-1].split('.'))
    '''print(list1)'''
    for l in list1:
        tokens.append(l[-2])
    for word in tokens:
        if word in no_use.keys():
            no_use[word]+=1     
        else:
            no_use[word]=1
    return no_use
print(mining_webpage())

I know this maybe has a simple solution but I really can't figure out what I am doing wrong, this is what I'm writing to practice.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    You are not passing any arguments to the `mining_webpage()` function while you've defined it with 2 arguments `url` and `list` – Keyur Potdar Jan 19 '18 at 08:22
  • 3
    Possible duplicate of [Function missing 2 required positional arguments: 'x' and 'y'](https://stackoverflow.com/questions/18940249/function-missing-2-required-positional-arguments-x-and-y) – Keyur Potdar Jan 19 '18 at 08:23
  • If even though I'm passing I'm getting it is not defined – Shailesh Singh Jan 19 '18 at 08:27

1 Answers1

0

In this case, the error message itself is pretty self-explanatory:

TypeError: mining_webpage() missing 2 required positional arguments: 'url' and 'list'

You've created a new function mining_webpage that expects two arguments url and list. That means that each time it is called, you will need to pass it two arguments. E.g.

my_list = []
print(mining_webpage('http://stackoverflow.com', my_list))

Alternatively, you may want to redefined mining_webpage so that it takes fewer arguments, or has default values that will be used if the function is called with fewer arguments than expected. An example with default values might look like:

from bs4 import BeautifulSoup as bs
...
import matplotlib as plt

def mining_webpage(url='http://stackoverflow.com', list=None):
   '''Finds the howmany websites are used in the webpage and counts its total number'''
    reallink=[]
    ...

Of course, setting default values in this way only makes sense if there are "default" values that you'd expect to be used frequently.

As a final note, it's not obvious that list is actually used for anything at all, and I'd really avoid calling any variable list at all since this is already defined in the Python standard library (https://docs.python.org/3/library/functions.html#func-list). This second argument appears to be used as the value in the assignment of a new variable word_list, which is then never referred to again.

Saff
  • 501
  • 3
  • 13