本文主要是介绍Python爬虫程序出错,求助,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
初学Python爬虫,运行出错
程序如下:
import requests
from lxml import etree
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537'
}
#获取小说想要爬取的urls
urls = ['http://www.bxwx123.com/novel/Lq3YH4/{}.html'.format(i) for i in range(1, 234)]
#设置保存小说路径
path = r'D:\\xiaoshuo\\'
#获取小说内容
def get_text(url):r = requests.get(url)r.encoding = 'utf-8'selector = etree.HTML(r.text)#获取文章标题title = selector.xpath('//*[@class="content"]/h1/text()')#获取小说正文内容text = selector.xpath('//*[@class="showtxt"]/text()')with open(path + title[0], 'w', encoding='utf-8') as f:for i in text:f.write(i)if __name__ == "__main__":for url in urls:get_text(url)
运行后出现如下错误:
请高手大大帮忙检查下,感激不尽!
这篇关于Python爬虫程序出错,求助的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!