Python爬虫取内容

很简短的代码,就这么轻松。以后用Python爬虫还是很方便的


import requests 
import parsel   

url = "https://read.qidian.com/chapter/WabWC9H2Nk3u4xLcYRGW6w2/1OKBFbasoeL4p8iEw--PPw2/"

response = requests.get(url) 

# 如遇乱码,则去找乱码的字符集  再使用  response.encoding = 'utf-8' / 'gbk' 转码
selector =  parsel.Selector(response.text)

title = selector.css('.text-head > h3 > span.content-wrap::text').get() # 截取标题
content_list = selector.css('#j_719261579').getall()    # 截取内容
content = ''.join(content_list)     # 将内容转为字符串

#   写入文件
with open(title + '.txt', 'w', encoding='utf-8') as f:
    f.write(title)
    f.write('\n')
    f.write(content)

你可能感兴趣的:(爬虫,python)