1.圖片搜尋
from flask import Flask, request, jsonify
import requests
from bs4 import BeautifulSoup
app = 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, jsonify
import requests
from bs4 import BeautifulSoup
app = 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=1
try:
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, jsonify
import mechanicalsoup
app = 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=1
try:
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, jsonify
from bs4 import BeautifulSoup
import json
import pyhttpx
app = 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, jsonify
from translate import Translator
app = 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, jsonify
import mechanicalsoup
app = 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()