[Python] 纯文本查看 复制代码 import requests #pip install requests
import parsel #pip install parsel
import time
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
def get_response(html_url):
response = requests.get(url=html_url, headers=headers)
response.encoding = response.apparent_encoding
return response
print('正在执行get_response')
def save(name, content):
with open(name +'.txt', mode='a', encoding='utf-8') as f:
f.write(content)
f.write('\n')
time.sleep(10)
print('正在执行save')
def get_novel_url(html_url):
response = get_response(html_url)
selector = parsel.Selector(response.text)
name = 'ceshiname'
content_list = selector.css('#cke_1_contents > div > p:nth-child(12)::text').getall()
content = "".join(content_list)
save(name,content)
print('正在执行get_novel_url')
if __name__ == '__main__':
url ="https://www.dulifei.com/standard/detail/102"
get_novel_url(url)
print('正在执行主程序')
运行以后的顺序是:
正在执行save
正在执行get_novel_url
正在执行__name__
很奇怪get_novel_url为什么没有第一部运行
|