【scrapy爬虫】最新sogou搜狗搜索 机智操作绕过反爬验证码(搜狗微信公众号文章同理)

本文介绍了如何使用scrapy框架爬取搜狗搜索结果,详细讲述了在遇到反爬机制时,如何通过更换IP、伪装header、动态获取cookies等策略巧妙绕过验证码,成功爬取数据并避免触发反爬机制。
摘要由CSDN通过智能技术生成

前情提要

此代码使用scrapy框架爬取特定“关键词”下的搜狗常规搜索结果,保存到同级目录下csv文件。并非爬取微信公众号文章,但是绕过验证码的原理相同。如有错误,希望大家指正。

URL结构

https://www.sogou.com/web?query={
   关键词}&page={
   n}

开始爬取

scrapy常规操作就不唠叨了,上代码

class SougouSearchSpider(scrapy.Spider):
    name = 'sogou_search'
    allowed_domains = ['www.sogou.com']
    start_urls = ['https://www.sogou.com/web?query=python&page=1']

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url,
                                 callback=self.parse)

一顿操作后,发现刚爬了3页,就停止了。报错如下

2020-06-11 16:05:15 [scrapy.downloadermiddlewares.redirect] DEBUG: Redirecting (302) to <GET http://www.sogou.com/antispider/?from=%2Fweb%3Fquery%3Dpython%26page%3D7%26ie%3Dutf8&suuid=6969bcac-7309-410c-b2f0-67d2e882e8a5&m=1> from <GET https://www.sogou.com/web?query=python&page=4&ie=utf8>

应该访问的第四页被302重定向到了antispider反爬虫的验证码界面
验证码
看到这里,我们采取一下常规措施:
1.更换动态IP
2.伪装header
3.硬刚破解验证码

一步一步来,我们先更换IP。这里我使用的是使用Redis的免费代理IP池:ProxyPool,Github地址。非常nice,代码十分简练。

import requests
proxypool_url = 'http://127.0.0.1:5555/random'
def get_random_proxy():
    response = requests.get(proxypool_url)
    try:
        if response.status_code == 200:
            return response.text.strip(
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
好的,下面是一个基于Scrapy框架开发的微信公众号爬虫程序的示例代码: 首先,我们需要在命令行中安装Scrapy和其他相关依赖: ``` pip install scrapy pip install scrapy-user-agents pip install scrapy-redis pip install scrapy-splash ``` 然后,创建一个新的Scrapy项目: ``` scrapy startproject wechat_spider ``` 接下来,进入项目目录并创建一个Spider: ``` cd wechat_spider scrapy genspider wechat_spider weixin.sogou.com ``` 在Spider中,我们需要定义要爬取的URL和页面元素的解析方法。下面是示例代码: ```python import scrapy from scrapy_splash import SplashRequest from scrapy_redis.spiders import RedisSpider class WechatSpider(RedisSpider): name = 'wechat_spider' allowed_domains = ['weixin.sogou.com'] start_urls = ['https://weixin.sogou.com/'] def parse(self, response): # 构造搜狗微信搜索的URL keyword = 'Python' url = f'https://weixin.sogou.com/weixin?type=1&s_from=input&query={keyword}&ie=utf8&_sug_=n&_sug_type_=' yield SplashRequest(url, self.parse_search_result, args={'wait': 10}) def parse_search_result(self, response): # 解析搜狗微信搜索结果页面 articles = response.css('.news-box .news-list li .txt-box h3 a::attr(href)').getall() for article_url in articles: yield SplashRequest(article_url, self.parse_article, args={'wait': 10}) def parse_article(self, response): # 解析公众号文章页面 title = response.css('#activity-name::text').get() content = response.css('#js_content').get() yield {'title': title, 'content': content} ``` 在这个示例中,我们使用了Scrapy-Redis、Scrapy-Splash和Scrapy-User-Agents等扩展库。我们首先在parse方法中构造搜狗微信搜索的URL,并使用SplashRequest发送请求。在parse_search_result方法中,我们解析搜狗微信搜索结果页面,获取每篇文章的URL,并再次使用SplashRequest发送请求。最后,在parse_article方法中,我们解析公众号文章页面,获取文章标题和内容,并通过yield返回给Scrapy框架。 在运行爬虫之前,我们需要在settings.py中配置Redis和Splash的相关参数: ```python # Redis配置 REDIS_HOST = 'localhost' REDIS_PORT = 6379 REDIS_PARAMS = {'password': 'your_password'} # Splash配置 SPLASH_URL = 'http://localhost:8050' # 下载中间件配置 DOWNLOADER_MIDDLEWARES = { 'scrapy_user_agents.middlewares.RandomUserAgentMiddleware': 400, 'scrapy_splash.SplashCookiesMiddleware': 723, 'scrapy_splash.SplashMiddleware': 725, 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810, } # 爬虫中间件配置 SPIDER_MIDDLEWARES = { 'scrapy_splash.SplashDeduplicateArgsMiddleware': 100, } # Splash参数配置 SPLASH_ARGS = { 'wait': 5, 'images': 0, 'render_all': 1, 'lua_source': """ function main(splash, args) splash.private_mode_enabled = false assert(splash:go(args.url)) assert(splash:wait(args.wait)) return splash:html() end """, } ``` 最后,使用以下命令启动爬虫: ``` scrapy crawl wechat_spider ``` 这是一个简单的微信公众号爬虫程序示例,你可以根据自己的需求进行修改和扩展。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彡千

赏杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值