Python怎么把输出整体写入CSV文件
1个回答
展开全部
# -*- coding:utf-8 -*-
import re
from urllib2 import urlopen
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
onehtml = urlopen("http://www.zhujiage.com.cn/article/List_2.html").read()
oneres = r'<A class=.{13} href=.(http://www.zhujiage.com.cn/article/.{6}/.{6}.html). title='
onecontent = re.findall(oneres,onehtml,re.S)
con = []
for i in range(1,31):
html = urlopen(onecontent[i]).read()
onejiexi = BeautifulSoup(html,'lxml')
try:
onesoup = onejiexi.findAll(id="content")[0]
con.append(onesoup.text)
res = '</a><a href=.(\d{6}_\d+.html).>'
content = re.findall(res,html,re.S)
new_content = sorted(set(content),key=content.index)
for item in new_content:
itemhtml = urlopen("http://www.zhujiage.com.cn/article/201607/" + item)
jiexi = BeautifulSoup(itemhtml,'lxml')
soup = jiexi.findAll(id="content")[0]
con.append(soup.text)
except IndexError:
continue
f = open(r'f:/T.txt', 'w')
for t in range(len(con)):
zhujia = con[t] + "\n"
f.write(zhujia)
f.close()
import re
from urllib2 import urlopen
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
onehtml = urlopen("http://www.zhujiage.com.cn/article/List_2.html").read()
oneres = r'<A class=.{13} href=.(http://www.zhujiage.com.cn/article/.{6}/.{6}.html). title='
onecontent = re.findall(oneres,onehtml,re.S)
con = []
for i in range(1,31):
html = urlopen(onecontent[i]).read()
onejiexi = BeautifulSoup(html,'lxml')
try:
onesoup = onejiexi.findAll(id="content")[0]
con.append(onesoup.text)
res = '</a><a href=.(\d{6}_\d+.html).>'
content = re.findall(res,html,re.S)
new_content = sorted(set(content),key=content.index)
for item in new_content:
itemhtml = urlopen("http://www.zhujiage.com.cn/article/201607/" + item)
jiexi = BeautifulSoup(itemhtml,'lxml')
soup = jiexi.findAll(id="content")[0]
con.append(soup.text)
except IndexError:
continue
f = open(r'f:/T.txt', 'w')
for t in range(len(con)):
zhujia = con[t] + "\n"
f.write(zhujia)
f.close()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询