zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python爬虫入门_在百度搜索手机归属地

2023-06-13 09:15:04 时间

枚举手机号,在百度搜索手机归属地

工具

  • requests http库
  • BeautifulSoup html解析库

代码

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup

headersPara = {    #伪装浏览器信息
    'Connection': 'Keep-Alive',
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
    'Accept-Encoding': 'gzip, deflate',
    'User-Agent': 'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
url="https://www.baidu.com/s"
f=open('./phonenumber.txt','w')

for i in range(111,119):
    word="13363460"+str(i)
    print(i)
    f.write(word+' ')
    data={
        'wd':word,
        'ie':'utf-8'
    }
    #发送get请求,添加可选参数params和headers
    response =requests.get(url=url,params=data,headers=headersPara)
    response.encoding="utf-8"
    #获取html网页
    html=response.text
    #使用lxml解析html页面成一棵树,返回给soup
    soup=BeautifulSoup(html,'lxml')
    #data=soup.select('#main > div > div.result-right > div.c-border.op_fraudphone_container > div > div.c-span21.c-span-last > div.op_fraudphone_row')
    #用select选择需要的标签,前面加.表示类名,可用空格组合过滤条件
    data=soup.select('.c-gap-bottom-small span')
    if data and data[1]:
        #print(data[1].get_text())
        #print(data[1].get('href'))
        #获取标签内的文本
        f.write(data[1].get_text())
    f.write('\n')

f.close()

参考

欢迎与我分享你的看法。 转载请注明出处:http://taowusheng.cn/