最新版本:自动批量外链发布工具
优采云 发布时间: 2022-12-14 17:44最新版本:自动批量外链发布工具
快速指南:
该接口允许用户批量导入链接,批量推送大量外部链接,蜘蛛可以通过该接口抓取用户的网站。导入TXT文档,一键推送。下面是批量发布外链的教程:
1.导出TXT文件
用户导入本地文件夹中写有网站链接的txt文档,txt文档每行一个链接,其他链接跳行输入。
2.批量推送
导入文档后,点击开始推送即可。
免费获取:python爬虫_微信公众号推送信息爬取的实例
问题描述
使用搜狗微信搜索抓取指定公众号的最新推送,并将对应网页保存到本地。
当心
搜狗微信获取的地址为临时链接,具有时效性。
公众号为动态网页(JavaScript渲染),使用requests.get()获取的内容不收录推送消息,这里使用selenium+PhantomJS处理
编码
#! /usr/bin/env python3
from selenium import webdriver
from datetime import datetime
import bs4, requests
import os, time, sys
# 获取公众号链接
def getAccountURL(searchURL):
res = requests.get(searchURL)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "lxml")
# 选择第一个链接
account = soup.select('a[uigs="account_name_0"]')
return account[0]['href']
# 获取首篇文章的链接,如果有验证码返回None
def getArticleURL(accountURL):
browser = webdriver.PhantomJS("/Users/chasechoi/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs")
# 进入公众号
browser.get(accountURL)
# 获取网页信息
html = browser.page_source
<p>
accountSoup = bs4.BeautifulSoup(html, "lxml")
time.sleep(1)
contents = accountSoup.find_all(hrefs=True)
try:
partitialLink = contents[0]['hrefs']
firstLink = base + partitialLink
except IndexError:
firstLink = None
print('CAPTCHA!')
return firstLink
# 创建文件夹存储html网页,以时间命名
def folderCreation():
path = os.path.join(os.getcwd(), datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
print("folder not exist!")
return path
# 将html页面写入本地
def writeToFile(path, account, title):
myfile = open("{}/{}_{}.html".format(path, account, title), 'wb')
myfile.write(res.content)
myfile.close()
base ='https://mp.weixin.qq.com'
accountList = ['央视新闻', '新浪新闻','凤凰新闻','羊城晚报']
query = 'http://weixin.sogou.com/weixin"#{}({}/{}): {}".format(account, index+1, len(accountList), accountURL))
# 读取第一篇文章内容
res = requests.get(articleURL)
res.raise_for_status()
detailPage = bs4.BeautifulSoup(res.text, "lxml")
title = detailPage.title.text
print("标题: {}\n链接: {}\n".format(title, articleURL))
writeToFile(path, account, title)
else:
print('{} files successfully written to {}'.format(index, path))
sys.exit()
print('{} files successfully written to {}'.format(len(accountList), path))</p>
参考输出
终端输出
发现者
分析
链接获取
首先进入搜狗的微信搜索页面,在地址栏中提取需要的链接,将公众号名称与字符串连接生成请求链接
对于静态网页,使用requests获取html文件,然后使用BeautifulSoup选择需要的内容
对于动态网页,使用selenium+PhantomJS获取html文件,然后使用BeautifulSoup选择需要的内容
遇到验证码(CAPTCHA)时,输出提示。这个版本的代码实际上并没有处理验证码。需要手动访问然后运行程序才能避开验证码。
文件写入
使用 os.path.join() 构建存储路径可以提高通用性。例如,Windows 路径分隔符使用反斜杠(\),而 OS X 和 Linux 使用正斜杠(/),此功能可以根据平台自动转换。
open()使用b(二进制模式)参数提高通用性(适配Windows)
使用datetime.now()获取当前时间进行命名,通过strftime()格式化时间(函数名中的f代表格式),
具体用法参考下表(摘自Automate the Boring Stuff with Python)
以上python爬虫_微信公众号推送信息爬取实例就是小编分享的全部内容,希望能给大家一个参考,也希望大家多多支持。