Skip to content

Instantly share code, notes, and snippets.

@ChiChou
Created August 13, 2016 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChiChou/d3d90b026f45017a98e8e39157d3caae to your computer and use it in GitHub Desktop.
Save ChiChou/d3d90b026f45017a98e8e39157d3caae to your computer and use it in GitHub Desktop.
How I name my project
#!/usr/bin/env python3
# git clone https://github.com/dwyl/english-words.git
# cd english-words
# wget https://gist.github.com/ChiChou/d3d90b026f45017a98e8e39157d3caae/raw/nameit.py
# pip3 install lxml beautifulsoup4 requests
# ./nameit.py
import random
import os
import requests
from bs4 import BeautifulSoup
wordlists = ('words.txt', 'words2.txt', 'words3.txt')
filename = random.choice(wordlists)
size = os.path.getsize(filename)
with open(filename) as f:
offset = random.randint(0, size)
f.seek(offset)
drop, word, *_ = f.read(64).split('\n')
print(word)
r = requests.get('http://cn.bing.com/dict/search', params={'q': word})
bs = BeautifulSoup(r.text, 'lxml')
definition = bs.select('.qdef ul li')
if not len(definition):
print('(未找到可用翻译)')
for li in definition:
print(' | '.join([li.select(selector)[0].text for selector in ('.pos', '.def')]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment