1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| __author__ = 'ypw'
from urllib import * from progressbar import * from bs4 import BeautifulSoup import socket import os
url = "http://www.xiexingcun.com/Global/xingxinyi/1.htm" path = "x"
socket.setdefaulttimeout(3) if os.path.exists(path) is False: os.makedirs(path) html = urlopen(url).read() soup = BeautifulSoup(html)
links = soup.find_all('a', target="_parent") index = 0 pbar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], maxval=len(links)).start()
for link in links: index += 1 html2 = html = urlopen("http://www.xiexingcun.com/Global/xingxinyi/" + link['href']).read() soup2 = BeautifulSoup(html2) title = soup2.find("font", size="4").get_text() text = soup2.find("font", size="2").get_text() f = open(path+"/" + title + ".txt", "w") f.write(text.encode("utf-8")) f.close() pbar.update(index) pbar.finish()
|