吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2689|回复: 0
收起左侧

[Python 转载] 某src公益厂商域名爬取

[复制链接]
ofk 发表于 2019-3-20 13:48
本帖最后由 ofk 于 2019-3-20 13:51 编辑

新人发个帖,以免被删了,哈哈哈。
首先,看看我们需要爬取得网站,
url:https://butian.360.cn/Reward/plan
5.jpg
使用fiddler抓包分析一波,开启浏览器代{过}{滤}理,点击公益厂商这个按钮:
2.png
可以看到链接为http://butian.360.cn/Reward/pub,向服务器请求了三个参数,一个s,一个p,一个token,再看看这个参数的含义,访问第二页看看,参数变化:
3.jpg
发现参数p的值发生了变化,变为2,所以p参数就是页码了,其他的参数就不用再了解了,知道了页码,就可以爬取不同页码的链接,
我们需要爬取的是域名,看看域名所在的地方:
4.jpg
这里有个cid参数,这个参数的值代表了某个厂商,把链接记下来:
https://butian.360.cn/Loo/submit?cid=xx
xx是需要自己去填入,填入后就可获得域名了。
xx在哪提取呢?看看
6.jpg
xx的值可在json数据中提取,
开始编写代码吧:
[Python] 纯文本查看 复制代码
#coding:utf-8
from urllib import request,parse
import re

url = "http://butian.360.cn/Reward/pub"
data = {
        's':1,
        'p':1,
        'token':''
}
headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': '你的cookie',
        'Host': 'butian.360.cn',
        'Referer': 'https://butian.360.cn/',
        'Upgrade-Insecure-Requests': 1,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 QIHU 360EE'
}
data = parse.urlencode(data).encode('utf-8')#对post请求参数进行url编码
changshang = request.Request(url, data = data, headers=headers, method='POST')
respones = request.urlopen(changshang).read().decode('utf-8')
#正则匹配需要的内容----cid值
parrtern = re.compile(r'\"company_id\":\"(\d*?)\"')
text = parrtern.findall(respones)

for i in range(len(text)):
        
        url = "https://butian.360.cn/Loo/submit?cid="+text[i]
        yuming = request.Request(url,headers=headers)
        respones = request.urlopen(yuming).read().decode('utf-8')
        #正则匹配需要的内容----域名和厂商
        parrtern1 = re.compile(r'<span>厂商名称:.*?value="(.*?)"')
        parrtern2 = re.compile(r'<span>所属域名:.*?value="(.*?)"')
        text1 = parrtern1.findall(respones)
        text2 = parrtern2.findall(respones)
        print(text1[0]+'-----------'+text2[0])


先爬第一页看看:
7.jpg

成功爬取了一页的厂商域名,
现在改一下代码,让代码有一点交互性:
[Python] 纯文本查看 复制代码
#coding:utf-8import re
from urllib import request,parse
import gzip


#爬取公益厂商的id
def get_Con(ye):

        url = "http://butian.360.cn/Reward/pub"
        data = {
                's':1,
                'p':ye,
                'token':''
        }
        
        data = parse.urlencode(data).encode('utf-8')#对post请求参数进行url编码
        changshang = request.Request(url, data = data, headers=headers, method='POST')
        respones = request.urlopen(changshang).read().decode('utf-8')
        #正则匹配需要的内容----cid值
        parrtern = re.compile(r'\"company_id\":\"(\d*?)\"')
        text = parrtern.findall(respones)
        # print(text)
        return text
        

def get_Url(id):
        global count
        url = 'http://butian.360.cn/Loo/submit?cid='+id
        yuming = request.Request(url,headers=headers)
        respones = request.urlopen(yuming).read()
        #由于网页时gzip编码的,所以需要解码
        f = gzip.decompress(respones).decode('utf-8')
        #正则匹配需要的内容----域名和厂商
        parrtern1 = re.compile(r'<span>厂商名称:.*?value="(.*?)"')
        parrtern2 = re.compile(r'<span>所属域名:.*?value="(.*?)"')
        text1 = parrtern1.findall(f)
        text2 = parrtern2.findall(f)

        data = text1[0] + '------' + text2[0]
        # print(data)
        filename.write(data+'\n')
        count = count + 1

        




headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': '你的cookie',
        'Host': 'butian.360.cn',
        'Referer': 'https://butian.360.cn/',
        'Upgrade-Insecure-Requests': 1,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 QIHU 360EE'
}

x = int(input('请输入开始页码:'))
y = int(input('请输入结束页码:'))

#创建一个文件
filename = open('butian.txt','a')
count = 0
def main():
        for i in range(x,y+1):
                id = get_Con(i)
                for i in id:
                        get_Url(i)
        print("爬取了%d个"%(count))
        print("------爬取完毕------")
        filename.close()
                

if __name__ == "__main__":
    main()
    
运行后就会在目录下生产一个butian.txt,里面是爬取的厂商域名
原本想使用多线程的,但是写完发现爬不到,并且卡死,所以就没有使用多线程。
可能是我的代码死锁了。
本代码没有cookie,所以cookie自己填入就可以了。有哪些不对的地方请各位大佬指出,学习学习!

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 02:18

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表