python操作随笔

# -*- encoding: utf-8 -*-
import urllib2
from bs4 import BeautifulSoup
import re

f1 = open('E:/1.txt')
line = f1.readline() # 调用文件的 readline()方法
list1 = []
while line:
list1.append(line)
line = f1.readline()
#print line

f = open('E:/txt1.txt', 'a')
for ele in list1:
url = 'https://cn.bing.com/dict/search?q='+ele+''
response = urllib2.urlopen(url)
dd = response.read()
s = re.search('更多例句筛选(.*)sb_pagS', dd)
mm = s.group()
soup = BeautifulSoup(mm, "html.parser")
listurl = soup.find_all(['span', 'a'])
str = ''
get = ''

times = 0
ifprit = 0
for url in listurl:
times += 1
if url.attrs.has_key(u'class') == True:
if url.attrs[u'class'] == [u'p1-3']:
continue

get = url.get_text()
if get.strip() == '':
continue
d = get[0]
if ord(d) < 300:
str += get + " "
ifprit = 1
else:
if ifprit == 1:
ifprit = 0
try:
f.write(str)
except:
continue
str = '\n '
get = ''
print ele
f.close()
posted @ 2018-06-07 15:33  第一v最寂寞  阅读(206)  评论(0编辑  收藏  举报