本帖最后由 949825667@qq.co 于 2016-8-25 10:23 编辑
自己用正则匹配了一下,没有得到完美的效果,有木有更好的正则匹配
我这个写法会连<a href="javascript:void(0);"class="century21" title="来源于21世纪大英汉词典"> </a>也匹配到- import urllib.request
-
- import bs4
- import re
-
-
- def trans(f):
- searchUrl = "http://dict.youdao.com/search?q=" + f + "&keyfrom=dict.index"
- web = urllib.request.urlopen(searchUrl)
- soup = bs4.BeautifulSoup(web, "html.parser")
- homegrids = soup.find(id='wordGroup')
- a = homegrids.find_all('a')
- print(homegrids.find_all('a'))
- for i in a:
- print(i.string)
- for j in homegrids:
- results = re.findall(r'.*</span>(.*)</p>', str(j), re.S)
- #print(str(j))
- for result in results:
- print(result)
- def main():
- f = input('')
- trans(f)
-
- if __name__ == '__main__':
- main()
复制代码
|