-*-coding:utf-8-*-
import requests
from lxml import html
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
url = "https://k.autohome.com.cn"
con = requests.get(url).content
sel = html.fromstring(con)
for i in sel.xpath('//li[@class="kb-list-item"]'):
try:
issue=i.xpath('/p[@class="item-title"]/text()')[0]
info=i.xpath('/p[@class="item-intr"][1]/text()')[0]
comment=i.xpath('/div[@class="item-info"]/span[@class="message"]/text()')[0]
agree=i.xpath('/div[@class="item-info"]/span[@class="like"]/text()')[0]
poster=i.xpath('/div[@class="item-info"]/span[@class="nick-name"]/text()')[0]
except IndexError:
print "indexerror"
print issue,info,comment,agree,poster
with open("autohome.txt","a") as f:
f.write("%s\n%s\n%s\n %s\n%s\n" % (issue,info,comment,agree,poster))
f.write("==========================\n")
程序没有报错,却什么都没有输出,一个数据都没有?这该怎么解决?