首页 编程教程正文

小白第一次独立写完代码,嘻嘻,爬诗词

piaodoo 编程教程 2020-02-22 22:09:06 945 0 python教程

本文来源吾爱破解论坛

不多比比,直接发源码,嘻嘻,小白一起交流学习,大佬多多给点建议和指导,谢谢!!!@.@
[Python] 纯文本查看 复制代码

# 作者:Administrator
# 日期:2019/7/26 23:38
# 工具:PyCharm
import requests
from lxml import etree
from pool import headers
def get_url():
        header = headers()
        page = int(input('请输入要爬取的页数:'))
        for j in range(1,page):
                try:
                        url = 'http://www.shicimingju.com/chaxun/zuozhe/13046_'+str(j)+'.html'
                        response = requests.get(url,headers=header)
                        html = etree.HTML(response.content.decode())
                        new_url = html.xpath('//div[@class="www-shadow-card www-main-container"]/h3/a/@href')
                        for i in new_url:
                                get_datas(i)
                except Exception as err:
                        print(err)


def get_datas(i):
        header = headers()
        url = 'http://www.shicimingju.com'+i
        response = requests.get(url, headers=header)
        html = etree.HTML(response.content.decode('utf-8'))
        title = ''.join(html.xpath('//div[@class="shici-container www-shadow-card"]/h1/text()'))
        content = '\n'.join(html.xpath('//div[@class="shici-content"]/text()')).strip()
        save_datas(title,content)



def save_datas(title,content):

        print('正在下载的诗词名字是:%s'%title)
        with open('gushi.txt','a',encoding='utf-8') as f:
                f.write('     《'+title+'》'+'\n')
                f.write(content+'\n')
                f.write('-'*60+'\n')

if __name__ == '__main__':
        get_url()



那个啥,浏览器代{过}{滤}理池,小白可以看一下
[Python] 纯文本查看 复制代码
import random
def headers():
    user_agent = [
        "{'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'}",
        "{'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)'}",
        "{'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)'}",
        "{'User-Agent':'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)'}",
        "{'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)'}",
        "{'User-Agent':'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'}",
        "{'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)'}",
        "{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)'}",
        "{'User-Agent':'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6'}",
        "{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1'}",
        "{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0'}",
        "{'User-Agent':'Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5'}",
        "{'User-Agent':'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6'}",
        "{'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'}",
        "{'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20'}",
        "{'User-Agent':'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52'}",
    ]
    header = random.choice(user_agent)
    return header

QQ截图20190727111028.png (30.88 KB, 下载次数: 0)

下载附件  保存到相册

2019-7-27 11:11 上传

QQ截图20190727111028.png
点评 LGLG 鼓励小白学习与实践  发表于 2019-7-27 11:44

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/7703.html

搜索