当前位置: 欣欣网 > 码农

6个 AI插件(自用)

2024-02-29码农

1.图片搜索

from flask import Flask, request, jsonifyimport requestsfrom bs4 import BeautifulSoupapp = Flask(__name__)def get_image_links_by_keyword(keyword, page_num=1): base_url = "https://www.toopic.cn/index.php/index_index_soso" image_links = []for i inrange(1, page_num+1):params = {'kw': keyword, 'page': i} response = requests.get(base_url, params=params) soup = BeautifulSoup(response.content, 'html.parser') img_tags = soup.find_all('img') img_sources = [img['src'] for img in img_tags]for src in img_sources: image_links.append("https://www.toopic.cn/" + src)return image_links@app.route('/', methods=['GET'])def get_image_links(): keyword = request.args.get('keyword') page_num = request.args.get('page_num')if not keyword:return"没有关键词"if page_num: page_num = int(page_num)else: page_num = 1 image_links = get_image_links_by_keyword(keyword, page_num)return jsonify({"image_links": image_links})if __name__ == '__main__': app.run(debug=True)

2.搜狗搜索

from flask import Flask, request, jsonifyimport requestsfrom bs4 import BeautifulSoupapp = Flask(__name__)@app.route('/', methods=['GET'])def search_sogou(): keyword = request.args.get('keyword') num_pages = request.args.get('num_pages')if not keyword :return jsonify({"error": "没有提供关键词!"})if not num_pages: num_pages=1try: num_pages = int(num_pages) except ValueError:return jsonify({"error": "页数必须是数字"}) base_url = "https://www.sogou.com/web?query={keyword}&page={page}" headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36' } search_results = []for page in range(1, num_pages + 1): url = base_url.format(keyword=keyword, page=page) response = requests.get(url, headers=headers)if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') results = soup.find_all('h3', class_='vr-title')for result in results: title_element = result.find('a')if title_element: title = title_element.text.strip() link = "https://www.sogou.com" + title_element['href'] search_results.append({"title": title, "link": link})else:return jsonify({"error": "Failed to fetch search results."})return jsonify(search_results)if __name__ == '__main__': app.run()

3.python库搜索

from flask import Flask, request, jsonifyimport mechanicalsoupapp = Flask(__name__)@app.route('/', methods=['GET'])defsearch_python_libraries(): keyword = request.args.get('keyword') num_pages = request.args.get('num_pages')ifnot keyword :return jsonify({"error": "没有keyword参数,输入关键词"})ifnot num_pages: num_pages=1try: num_pages = int(num_pages)except ValueError:return jsonify({"error": "页数必须是数字"}) browser = mechanicalsoup.StatefulBrowser() search_results = []for page in range(1, num_pages + 1): browser.open(f'https://pypi.org/search/?q={keyword}&page={page}') results = browser.get_current_page().select('.package-snippet')for result in results: title_element = result.select_one('.package-snippet__title') name = title_element.select_one('.package-snippet__name').text.strip() version = title_element.select_one('.package-snippet__version').text.strip() created = result.select_one('.package-snippet__created time')['datetime'] description_element = result.select_one('.package-snippet__description') description = description_element.text.strip() if description_element elseNone link = f"https://pypi.org/project/{name}/" search_results.append({"name": name,"version": version,"created": created,"description": description,"link": link })return jsonify(search_results)if __name__ == '__main__': app.run()

4.IMDB电影预告片

from flask import Flask, request, jsonifyfrom bs4 import BeautifulSoupimport jsonimport pyhttpxapp = Flask(__name__)@app.route('/', methods=['GET'])defget_movies(): output_count = request.args.get('count')if output_count and output_count.isdigit(): output_count = int(output_count)else: output_count = None headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" } session = pyhttpx.HttpSession() res = session.get(url='https://www.imdb.com/trailers/', headers=headers) soup = BeautifulSoup(res.text, 'html.parser') links = soup.find_all('a', class_='ipc-poster-card__title') images = soup.find_all('img', class_='ipc-image') movie_data = []for link, image in zip(links, images): href = link['href'] text = link.get_text() movie_info = {"movie": text,"url": f"https://www.imdb.com{href}","image": image["src"] } movie_data.append(movie_info)if output_count isnotNone: movie_data = movie_data[:output_count]return json.dumps(movie_data, ensure_ascii=False, indent=2)if __name__ == '__main__': app.run()

5.谷歌翻译

from flask import Flask, request, jsonifyfrom translate import Translatorapp = Flask(__name__)@app.route('/', methods=['GET'])deftranslate_text():try: text = request.args.get('text')ifnot text:return jsonify({"translated_text": "没有内容"}) from_lang = request.args.get('from_lang', 'zh') to_lang = request.args.get('to_lang', 'en') translator = Translator(from_lang=from_lang, to_lang=to_lang) translated_text = translator.translate(text)return jsonify({"translated_text": translated_text})except Exception as e:return jsonify({"error": str(e)})if __name__ == '__main__': app.run(debug=True)

6.百度搜索

from flask import Flask, request, jsonifyimport mechanicalsoupapp = Flask(__name__)@app.route('/', methods=['GET'])def search_baidu(): keyword = request.args.get('keyword')if not keyword:return jsonify({"error": "输入关键词"}) browser = mechanicalsoup.StatefulBrowser() browser.open("http://www.baidu.com") search_form = browser.select_form('#form') search_form['wd'] = keyword browser.submit_selected() page = browser.get_current_page() results = page.select('.result') search_results = []for result in results: title = result.select('h3')[0].text.strip() link = result.select('a')[0]['href'] search_results.append({"title": title,"link": link })return jsonify(search_results)if __name__ == '__main__': app.run()