Python 代码推送百度链接(python代码大全)

网友投稿 252 2022-09-02


Python 代码推送百度链接(python代码大全)

通过代码实现抓取个人博客中某一页指定文章链接,并批量将该链接推送到百度站长平台,起到快速收录的目的。

import sysimport requestsfrom bs4 import BeautifulSoup# 推送百度爬虫def push_page(url): headers = { 'User-Agent': 'curl/7.12.1', 'Host': 'data.zz.baidu.com', 'Content-Type': 'text/plain', 'Content-Length': '83' } urls = " try: html = requests.post(urls, headers=headers, data=url, timeout=5).text push_status = eval(html)['success'] if push_status == 1: return 1 else: return 0 except: return 0# 获取路径def get_page(page): html = requests.get(page,timeout=5).text try: bs = BeautifulSoup(html,"html.parser") ret = bs.select('div[class="container"] div[class="row"] h2[class="post-title"] a') for item in ret: push_url = item.get('href') push_ref = push_page(push_url) print("推送: {} --> 状态: {}".format(push_url,push_ref)) return 1 except: return 0if __name__ == "__main__": arg = sys.argv get_page(arg[1])

2.0批量推送

import requestsfrom bs4 import BeautifulSoup# 推送百度爬虫def push_page(url): headers = { 'User-Agent': 'curl/7.12.1', 'Host': 'data.zz.baidu.com', 'Content-Type': 'text/plain-t', 'Content-Length': '83' } urls = " try: html = requests.post(urls, headers=headers, data=url, timeout=5).text push_status = eval(html)['success'] if push_status == 1: print("推送页面: {} 推送状态: {}".format(url,push_status)) return 1 else: print("推送页面: {} 推送状态: {}".format(url, push_status)) return 0 except: return 0# 获取路径def get_page(page): push_url_list = [] html = requests.get(page,timeout=5).text try: bs = BeautifulSoup(html,"html.parser") ret = bs.select('div[class="container"] div[class="row"] h2[class="post-title"] a') for item in ret: push_url = item.get('href') print("提取链接: {}".format(push_url)) push_url_list.append(push_url) return push_url_list except: return 0# 生成所有页面链接def create_page(start,end): page_list = [] for ea in range(start,end+1): page = f" print("创建页面链接: {}".format(page)) page_list.append(page) return page_listif __name__ == "__main__": while True: push_url = [] # 生成页面目录 page = create_page(1,15) for each in page: # 输出每一个链接目录树 ref = get_page(each) push_url.extend(ref) # 开始批量推送 for url in push_url: push_page(url)


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python 运维CMD交互控制台 [应用](python基础教程)
下一篇:Java IO之File 类详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~