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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6382|回复: 48
收起左侧

[Python 转载] seo站点关键词爬虫

   关闭 [复制链接]
lindsey 发表于 2022-1-5 21:57
本帖最后由 lindsey 于 2022-1-6 02:47 编辑

接了seo的爬虫单子,但是被放鸽子了,直接放上源码和打包好的exe,希望有人可以用到
exe下载链接:https://pan.baidu.com/s/12Iy2PbXAl488TH2Ha1Neyg
提取码:6666
基于sogou的站点关键词查询,自动保存前200个关键词标题到excel,路径为当前路径
seo1.png

[Python] 纯文本查看 复制代码
import re
import aiohttp
import asyncio
import aiofiles
from fake_useragent import UserAgent
import requests

ua = UserAgent().random

class Spider(object):

    def __init__(self,cookies):
        self.headers = {'User-Agent': ua}
        self.cookies = cookies
        self.url = 'http://www.sogou.com/web'
        self.path = '.'                          #存储路径(默认当前路径)

    async def get_url(self, url,params):
        async with aiohttp.ClientSession() as client:
            async with client.get(url, headers=self.headers,params=params,cookies=self.cookies) as resp:
                if resp.status == 200:
                    return await resp.text()

    async def parsel_data(self,param,query):
        semaphore = asyncio.Semaphore(5)
        html = await self.get_url(self.url,param)
        excel_path = '/'.join([self.path,f'{query}.xlsx'])
        data_list = re.findall('""><!--awbg\d+-->(.*?)</a>',html)
        tasks = [asyncio.create_task(self.write_data(excel_path,data,semaphore)) for data in data_list]
        await asyncio.wait(tasks)

    async def write_data(self,path,data,semaphore):
        async with semaphore:
            async with aiofiles.open(path, 'a') as f:
                print(data)
                data = data + '\n'
                await f.write(data)



    async def main(self,):
        query = input('请输入要查询的域名:')
        print('--------------------开始爬取-------------------')
        for page in range(1,21):
            params = {
                "query": f"site:{query}",
                'page': int(page)
            }
            await self.parsel_data(params,query)
        print('--------------------爬取完成-------------------')

def get_new_cookies():    #获取最新的cookie防止访问次数超限
    url = 'https://v.sogou.com/v?ie=utf8&query=&p=40030600'
    headers = {'User-Agent': ua}
    rst = requests.get(url=url,
                       headers=headers,
                       allow_redirects=False,)
    cookies = rst.cookies.get_dict()
    return cookies

if __name__ == '__main__':
    cookie = get_new_cookies()
    run = Spider(cookie)
    asyncio.run(run.main())

免费评分

参与人数 5吾爱币 +5 热心值 +4 收起 理由
yuxuechao + 1 我很赞同!
小小的石头13 + 1 + 1 我很赞同!
tomlong + 1 + 1 我很赞同!
狼爷 + 1 + 1 大哥牛逼
羿辰 + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

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

 楼主| lindsey 发表于 2022-1-6 02:48
HAHAKALAYO 发表于 2022-1-6 00:02
大佬这个代码如何使用呢,想试试,谢谢

我打包成exe了,直接下载使用
链接:https://pan.baidu.com/s/12Iy2PbXAl488TH2Ha1Neyg
提取码:6666
sooboo 发表于 2022-2-24 15:01
lindsey 发表于 2022-1-6 02:48
我打包成exe了,直接下载使用
链接:https://pan.baidu.com/s/12Iy2PbXAl488TH2Ha1Neyg
提取码:6666

挂了,能从新分享下么,感谢大佬
KangLi 发表于 2022-1-6 00:01
HAHAKALAYO 发表于 2022-1-6 00:02
大佬这个代码如何使用呢,想试试,谢谢
HAHAKALAYO 发表于 2022-1-6 08:44
lindsey 发表于 2022-1-6 02:48
我打包成exe了,直接下载使用
链接:https://pan.baidu.com/s/12Iy2PbXAl488TH2Ha1Neyg
提取码:6666

感谢大佬,祝你幸福
科西嘉滕 发表于 2022-1-6 08:52
感谢分享
丶夜猫 发表于 2022-1-6 09:11
搜狗现在用的人也蛮多的了
yeminch 发表于 2022-1-6 09:34
居然放鸽子,搞他丫的
薛定谔的猫0o0 发表于 2022-1-6 10:15
感谢分享
kafei000 发表于 2022-1-7 07:44
这样一个单子多少钱啊
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-5-16 15:13

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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