David
David

Reputation: 71

How to achieve this python post and get?

I've been trying for hours using requests and urllib. I'm so lost, misunderstood by google too. Some tips, or even anything would be useful. Thank you.

Goals: Post country code and phone numbers, then get mobile carrier etc. Problem: Not printing anything. Variable "name" prints out None.

def do_ccc(self): #Part of bigger class
    """Phone Number Scan"""
    #prefix=input("Phone Number Prefix:")
    #number=input("Phone Number: ")
    url=("https://freecarrierlookup.com/index.php")

    from bs4 import BeautifulSoup
    import urllib
    data = {'cc' : "COUNTRY CODE", 
            'phonenum' : "PHONE NUMBER"}#.encode('ascii')
    data=json.dump(data,sys.stdout)

    page=urllib.request.urlopen(url, data)
    soup=BeautifulSoup(page, 'html.parser')
    name=soup.find('div', attrs={'class': 'col-sm-6 col-md-8'})
    #^^^# Test(should print phone number)
    print(name)

Upvotes: 1

Views: 88

Answers (2)

Maximilian Peters
Maximilian Peters

Reputation: 31649

As Zags pointed out, it is not a good idea to use a website and violate their terms of service, especially when the site of offers a cheap API.

But answering your original question:

  • You are using json.loads instead of json.load resulting in an empty an empty data object.
  • If you look at the page, you will see that the URL for POST requests is different, getcarrier.php instead of index.php.
  • You would also need to convert your str from json.dumps to bytes and even then the site will reject your calls, since a hidden token is added to each request submitted by the website to prevent automatic scraping.

Upvotes: 1

Zags
Zags

Reputation: 41180

The problem is with what you're code is trying to do. freecarrierlookup.com is just a website. In order to do what you want, you'd need to do web scraping, which is complicated, unmaintainable, and usually a violation of the site's terms of service.

What you need to do is find an API that provides the data you're looking for. A good API will usually have either sample code or a Python library that you can use to make requests.

Upvotes: 0

Related Questions