芯幻

使用python实现自动点赞(优化)

学习区, 教程&技术 张下 - 1
解放双手,下瑟瑟再也不怕没积分了
import requests
from lxml import etree
from concurrent.futures import ThreadPoolExecutor

# 使用 session 来复用连接
session = requests.Session()

# 公共的请求头
headers = {
   
}

params = {
   
}

# 爬取页面并获取链接
def get_page_urls(page):
    xhurl = f’https://xhcya.com/category/acg/page/{page}’   #分区链接
    try:
        resp = session.get(url=xhurl, headers=headers).text
        tree = etree.HTML(resp)
        urls = tree.xpath(‘//div[@class=”inn-archive__item__container inn-card_post-thumbnail__item__container”]/a/@href’)
        return [url.split(‘/’)[-1] for url in urls]
    except Exception as e:
        print(f”Error fetching page {page}: {e}”)
        return []

# 发送 POST 请求
def post_data(post_id):
    try:
        data = f’——WebKitFormBoundary948aWGHc0Ar4BqoBrnContent-Disposition: form-data; name=”postId”rnrn{post_id}rn——WebKitFormBoundary948aWGHc0Ar4BqoB–rn’
        response = session.post(url=’https://xhcya.com/wp-admin/admin-ajax.php’, headers=headers, data=data, params=params)
        print(f”Response for {post_id}: {response.text}”)
    except Exception as e:
        print(f”Error posting data for {post_id}: {e}”)

# 主函数,使用多线程并发
def main():
    with ThreadPoolExecutor(max_workers=20) as executor:
        # 获取所有页面的链接
        futures = [executor.submit(get_page_urls, page) for page in range(1, 5)]    #爬取页码数
        post_ids = []
        for future in futures:
            post_ids.extend(future.result())

        # 对每个链接的ID进行并发POST请求
        post_futures = [executor.submit(post_data, post_id) for post_id in post_ids]图片
        for future in post_futures:
            future.result()  # 等待所有请求完成

if __name__ == “__main__”:
    main()

//之前用别人的脚本点赞觉得有点麻烦,所有自己就写了一个,大致就是爬取某分区的所有文章链接,然后点赞
版权所有 © 芯幻 2022

DMCA / Report Contact:i@acg.la