前言

近期有许多项目需要这个功能,由于Python实现起来比较简单就这么做了,代码贴下来觉得好点个赞吧~

代码

# coding: utf-8
import os
import time
import requests
import urllib.parse
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from fake_useragent import UserAgent
from multiprocessing.pool import ThreadPool LOCATIONS = {}
GLOBAL_THREAD = 500
GLOBAL_TIMEOUT = 50 def get_links(keyword, generator, pages):
links = [] for page in range(int(pages.split("-")[0]), int(pages.split("-")[1]) + 1):
for genera in range(int(generator.split("-")[0]), int(generator.split("-")[1]) + 1):
links.append(
"http://www.baidu.com.cn/s?wd=" + urllib.parse.quote(keyword + str(genera)) + "&pn=" + str(page * 10)) return links def get_page(url):
headers = {"user-agent": UserAgent().chrome}
req = requests.get(url, headers=headers)
req.encoding = "utf-8"
soup = BeautifulSoup(req.text, "lxml") for link in soup.select("div.result > h3.t > a"):
req = requests.get(link.get("href"), headers=headers, allow_redirects=False)
if "=" in req.headers["location"]:
root = urlparse(req.headers["location"]).netloc
LOCATIONS[root] = req.headers["location"] def baidu_search():
try:
os.system("cls")
print("-" * 56 + "\n")
print("| BaiduSearch Engine By 美图博客[https://www.meitubk.com/] |\n")
print("-" * 56 + "\n") keyword = input("Keyword: ")
generator = input("Generator(1-10): ")
pages = input("Pages(0-10): ") start = time.time()
pool = ThreadPool(processes=GLOBAL_THREAD)
pool.map(get_page, get_links(keyword, generator, pages))
pool.close()
pool.join()
end = time.time() path = r"D:\Desktop\result.txt"
save_result(path)
print("\nSava in %s" % path)
print("Result count: %d" % len(LOCATIONS.values()))
print("Running time: %ds" % (end - start))
except:
print("\nInput Error!")
exit(0) def save_result(path):
with open(path, "w") as file:
for url in list(LOCATIONS.values()):
file.write(url + "\n") baidu_search()

使用

用 Python 获取百度搜索结果链接的更多相关文章

  1. 【Python学习笔记六】获取百度搜索结果以及百度返回“百度安全验证”问题解决

    1.获取百度搜索结果页面主要是修改百度搜索url中的参数实现,例如查询的关键字为wd: 举例:https://www.baidu.com/s?wd=python",这样就可以查询到‘pyth ...

  2. python采集百度搜索结果带有特定URL的链接

    #coding utf-8 import requests from bs4 import BeautifulSoup as bs import re from Queue import Queue ...

  3. Python获取百度浏览记录

    Python模拟百度登录实例详解 http://www.jb51.net/article/78406.htm Python实战计划学习作业2-1 http://blog.csdn.net/python ...

  4. js 获取百度搜索关键词的代码

    有可能有时候我们会用到在百度搜什么关键词进来我们的网站的,所有我们又想拿到用户搜索的关键词. 这是我研究了半天所得出的办法.话不多说直接贴代码 <script> function quer ...

  5. jsonp模拟获取百度搜索相关词汇

    随便写了个jsonp模拟百度搜索相关词汇的小demo,帮助新手理解jsonp的用法. <!DOCTYPE html><html lang="en">< ...

  6. Python实现百度搜索并保存到本地示例,Python实现百度搜索

    实现百度搜索并保存到本地 User_Agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko ...

  7. 解析百度搜索结果链接的url,获取真正的url

    通常,在百度输入关键词搜索出现的列表页,点击目标链接,然而跳转的时候却是百度地址,经过百度解析,才真的跳到目标页面. 在SEO中,经常需要看下自己的网站排名,又不想手动每天手动去点,可用以下方法去得到 ...

  8. Python 版百度站长平台链接主动推送脚本

    如果自己的网站需要被百度收录,可以在搜索结果中找到,就需要将网站的链接提交给百度.依靠百度的爬虫可能无法检索到网站所有的内容,因此可以主动将链接提交给百度. 在百度的站长平台上介绍了链接提交方法,目前 ...

  9. python 模拟百度搜索

    import urllib.request def Url(url): flag = input("请输入要搜索的关键字:") headers_ = { "User-Ag ...

随机推荐

  1. Pandas 精简实例入门

    目录 0. 案例引入 1. Pandas 主要数据结构 1.1 DataFrame 1.1.1 设置索引 1.1.2 重设索引 1.1.3 以某列为索引 1.2 MultiIndex 1.3 Seri ...

  2. 【Pytest04】全网最全最新的Pytest框架fixture应用篇(2)

    一.Fixture参数之params参数可实现参数化:(可以为list和tuple,或者字典列表,字典元祖等) 实例如下: import pytest def read_yaml(): '] @pyt ...

  3. coding++:RateLimiter 限流算法之漏桶算法、令牌桶算法--简介

    RateLimiter是Guava的concurrent包下的一个用于限制访问频率的类 <dependency> <groupId>com.google.guava</g ...

  4. 线程安全,syncronized 用法

    1,为什么有线程安全问题? 当多个线程同时共享同一个全局变量或静态变量,做写的操作时,可能会发生数据冲突问题,也就是线程安全问题.但是做读操作是不会发生数据冲突问题. public class Tra ...

  5. nodejs使用express中静态资源托管(express.static())时遇到的bug

    如下:将test.html的页面挂载在服务器上, const express= require('express') const fs= require('fs') let app = express ...

  6. iOS UITableView优化

    一.Cell 复用 在可见的页面会重复绘制页面,每次刷新显示都会去创建新的 Cell,非常耗费性能.  解决方案:创建一个静态变量 reuseID,防止重复创建(提高性能),使用系统的缓存池功能. s ...

  7. spring boot 装载自定义yml文件

    yml格式的配置文件感觉很人性化,所以想把项目中的.properties都替换成.yml文件,蛋疼的是springboot自1.5以后就把@configurationProperties中的locat ...

  8. springboot系列(三)配置文件详解

    目录 properties 文件 1.语法 2.优先级 3.自定义数据配置 4.获取自定义数据配置 1.通过prefix获取 yml文件 1.语法 2.优先级 3.自定义数据配置. 4.获取自定义数据 ...

  9. Vertica的这些事(十一)——-Vertica备份元数据信息

    ---备份资源池 SELECT 'CREATE RESOURCE POOL ' || name || CASE WHEN memorysize IS NULL THEN ' ' ELSE ' MEMO ...

  10. escape和unescape知识点

    decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码. encodeURI() 函数可把字符串作为 URI 进行编码 <script> var uri= ...