raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 403: Forbidden
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'}
request = url_req.Request(url, headers=headers)
response = url_req.urlopen(request, data=None, timeout=3)
html = response.read().decode('GBK')
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 11 16:31:42 2017
@author: lart
"""
import urllib.request as url_req
import re, socket, time
def r_o_html(url):
print('r_o_html begin')
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'}
request = url_req.Request(url, headers=headers)
NET_STATUS = False
while not NET_STATUS:
try:
response = url_req.urlopen(request, data=None, timeout=3)
html = response.read().decode('GBK')
print('NET_STATUS is good')
print('r_o_html end')
return html
except socket.timeout:
print('NET_STATUS is not good')
NET_STATUS = False
def re_findall(re_string, operation, html):
print('re_findall begin')
pattern = re.compile(re_string, re.I)
if operation == 'findall':
result = pattern.findall(html)
else:
print('this operation is invalid')
exit(-1)
print('re_findall end')
return result
if __name__ == '__main__':
url_base = 'http://www.7kankan.la/book/1/'
html = r_o_html(url_base)
findall_title = re_findall(r'<title>(.+?)</title>', 'findall', html)
findall_chapter = re_findall(r'<dd class="col-md-3"><a href=[\',"](.+?)[\',"] title=[\',"](.+?)[\',"]>', 'findall', html)
with open(findall_title[0] + '.txt', 'w+', encoding='utf-8') as open_file:
print('article文件打开', findall_chapter)
for i in range(len(findall_chapter)):
print('第' + str(i) + '章')
open_file.write('\n\n\t' + findall_chapter[i][1] + '\n --------------------------------------------------------------------- \n')
url_chapter = url_base + findall_chapter[i][0]
html_chapter = r_o_html(url_chapter)
findall_article = re_findall(r' (.+?)<br />', 'findall', html_chapter)
findall_article_next = findall_chapter[i][0].replace('.html', '_2.html')
url_nextchapter = url_base + findall_article_next
html_nextchapter = r_o_html(url_nextchapter)
if html_nextchapter:
findall_article.extend(re_findall(r' (.+?)<br />', 'findall', html_nextchapter))
for text in findall_article:
open_file.write(text + '\n')
time.sleep(1)
print('文件写入完毕')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有