python - 如何实现这个python post并获取?

标签 python python-3.x python-requests urllib

我已经尝试使用 requestsurllib 几个小时了。我很迷失,也被谷歌误解了。一些技巧,甚至任何东西都会有用。谢谢。

目标:发布国家代码和电话号码,然后获取移动运营商等。 问题:不打印任何内容。变量“name”打印出 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)

最佳答案

正如 Zags 指出的那样,使用网站并违反其服务条款并不是一个好主意,特别是当该网站提供廉价 API 时.

但是回答你原来的问题:

  • 您使用 json.loads 而不是 json.load 导致空的 data 对象。
  • 如果您查看该页面,您会发现 POST 请求的 URL 不同,是 get Carrier.php 而不是 index.php
  • 您还需要将 strjson.dumps 转换为 bytes,即使这样,网站也会拒绝您的调用,因为隐藏 token 会添加到网站提交的每个请求中,以防止自动抓取。

关于python - 如何实现这个python post并获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732475/

相关文章:

Python 请求库结合了 HTTPProxyAuth 和 HTTPBasicAuth

python - 将 vpn 与 python 请求一起使用

Python 3 : how to scrape research results from a website using CSFR?

Python 和 Pygame 循环不递增

python - 为什么 .isascii() 在 google colab 上不起作用?

python - 为什么指定end关键字时python3的print语句不刷新输出?

python - Fiddler 可以更改 Python 请求模块的返回吗?

python - 如何在 CPLEX Python 中实现逻辑 OR 约束

python - 如何使用Python中的套接字库将服务器连接到其他计算机?

python - 使用 django-social-auth 出现导入错误