python刷简书文章访问量

#代码仅供学习和探讨,如果侵害利益,请联系我删除

      无意间发现简书刷新页面可以增加一个阅读量,然后去网上搜索,发现文章非常陈旧不堪,代码早已经失效,想找个现成的代码找了几个小时都找不到。然后放弃了,自己花时间研究了下。

主要方法有3种:

  • 第一种:利用requests请求,进行访问从而来提升阅读量,对增加1000+请求可以了.

  • 第二种:利用python 异步高并发请求,主要模块asyncioaiohttp,对增加10000+比较舒服

  • 第三种:多进程requestsmultiprocessing请求,推荐

  • 第四种:python3+selenium利用自动化框架进行请求,此乃下下策,之前用了多进程来操作10driver,发现增加速度上慢的太多,而且非常耗电脑资源。这种方法不推荐。

准备一个链接:https://www.jianshu.com/p/bec306f3210d
安装模块:pip install requests
不用异步下面异步模块不用安装

  1. pip install aiohttp
  2. pip install asyncio
    效果图演示
    python刷简书文章访问量_第1张图片
    python刷简书文章访问量_第2张图片

第一种方法:

普通post请求

import requests,json
import time

def read_add(url):
    url_new=url.split('/')[-1]
    _url ='https://www.jianshu.com/shakespeare/notes/{}/mark_viewed'.format(url_new)
    headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
    'Host': 'www.jianshu.com',
    'Referer': 'https://www.jianshu.com/p/{}'.format(url_new)}
    
    for _ in range(20):
        res=requests.post(_url,data=json.dumps({'fuck':1}),headers=headers)
        print(res)


url = 'https://www.jianshu.com/p/bec306f3210d'
read_add(url)

第二种方法

import requests,json
import asyncio
import time
import aiohttp
from multiprocessing import Pool

def read_add(url):
    url_new=url.split('/')[-1]
    _url ='https://www.jianshu.com/shakespeare/notes/{}/mark_viewed'.format(url_new)
    headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
    'Host': 'www.jianshu.com',
    'Referer': 'https://www.jianshu.com/p/{}'.format(url_new)}
    
    for _ in range(10000):
        res=requests.post(_url,data=json.dumps({'fuck':1}),headers=headers)
        print(res)


if __name__ == "__main__":
    p = Pool(4)
    for _ in range(12):
        url = 'https://www.jianshu.com/p/bec306f3210d'
        p.apply_async(read_add, args=(url,))
    p.close()
    p.join()

第三种方法

python 异步协程请求

import requests,json
import asyncio
import time
import aiohttp




async def post_html_text(url):
    url_new=url.split('/')[-1]
    _url ='https://www.jianshu.com/shakespeare/notes/{}/mark_viewed'.format(url_new)
    conn=aiohttp.TCPConnector(ssl=False,limit=100) #忽略证书错误
    async with aiohttp.ClientSession(connector=conn) as session: #cookies=cookies
        try:
            proxy = "https://114.98.24.169:4216"
            url ='https://www.jianshu.com/shakespeare/notes/bec306f3210d/mark_viewed'
            headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36','Host': 'www.jianshu.com','Referer': 'https://www.jianshu.com/p/{}'.format(url_new)}
            response = await session.post(url,data=json.dumps({'fuck':1}),timeout=30,headers=headers) #data=json.dumps(payload),headers=headers
            result = await response #await resp.text(encoding='windows-1251') 解码

            return result
        except:
            return 'Exceptional error occurred'


def main(url):
    tasks = [asyncio.ensure_future(post_html_text(url)) for page in range(10000) ]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))

if __name__ == "__main__":
    url = 'https://www.jianshu.com/p/bec306f3210d'
    main(url)

你可能感兴趣的:(Python,爬虫应用例子)