python基础爬虫,翻译爬虫,小说爬虫

基础爬虫:

# -*- coding: utf-8 -*-

import requests

url = 'https://www.baidu.com'  # 注释1
headers = {
    # 注释2
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) \
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
response = requests.get(url=url, headers=headers)  # 注释3
content = response.content.decode()  # 注释4
print(content)  # 注释5

翻译爬虫:

from urllib import request, parse
import json

if __name__ == '__main__':
    baseurl = "https://fanyi.baidu.com/sug"
    word = input("请输入您想输入的单词:")
    # 我们需要传送过去的数据
    datas = {
        'kw': word
    }
    # 对数据进行编码
    data = parse.urlencode(datas).encode()
    # 写http头部,至少需要Content-Length
    headers = {
        # 此处为编码后的长度
        'Content-Length': len(data),
    }
    # 将数据传送
    req = request.Request(url=baseurl, data=data, headers=headers)
    res = request.urlopen(req)

    json_data = res.read()
    json_data = json_data.decode()
    json_data = json.loads(json_data)
    # data里面是一个list
    data_list = json_data.get('data')
    for item in data_list:
        print(item['k'], '---', item['v'])

小说爬虫:

# -*- coding:UTF-8 -*-
from bs4 import BeautifulSoup
import requests, sys

"""
类说明:下载《笔趣看》网小说《一念永恒》
Parameters:
    无
Returns:
    无
Modify:
    2017-09-13
"""
class downloader(object):

    def __init__(self):
        self.server = 'https://www.biqukan.com/'
        self.target = 'https://www.biqukan.com/1_1094/'
        self.names = []            #存放章节名
        self.urls = []            #存放章节链接
        self.nums = 0            #章节数

    """
    函数说明:获取下载链接
    Parameters:
        无
    Returns:
        无
    Modify:
        2017-09-13
    """
    def get_download_url(self):
        req = requests.get(url = self.target)
        html = req.text
        div_bf = BeautifulSoup(html)
        div = div_bf.find_all('div', class_ = 'listmain')
        a_bf = BeautifulSoup(str(div[0]))
        a = a_bf.find_all('a')
        self.nums = len(a[15:])                                #剔除不必要的章节,并统计章节数
        for each in a[15:]:
            self.names.append(each.string)
            self.urls.append(self.server + each.get('href'))

    """
    函数说明:获取章节内容
    Parameters:
        target - 下载连接(string)
    Returns:
        texts - 章节内容(string)
    Modify:
        2017-09-13
    """
    def get_contents(self, target):
        req = requests.get(url = target)
        html = req.text
        bf = BeautifulSoup(html)
        texts = bf.find_all('div', class_ = 'showtxt')
        texts = texts[0].text.replace('\xa0'*8,'\n\n')
        return texts

    """
    函数说明:将爬取的文章内容写入文件
    Parameters:
        name - 章节名称(string)
        path - 当前路径下,小说保存名称(string)
        text - 章节内容(string)
    Returns:
        无
    Modify:
        2017-09-13
    """
    def writer(self, name, path, text):
        write_flag = True
        with open(path, 'a', encoding='utf-8') as f:
            f.write(name + '\n')
            f.writelines(text)
            f.write('\n\n')

if __name__ == "__main__":
    dl = downloader()
    dl.get_download_url()
    print('《一年永恒》开始下载:')
    for i in range(dl.nums):
        dl.writer(dl.names[i], '一念永恒.txt', dl.get_contents(dl.urls[i]))
        sys.stdout.write("  已下载:%.3f%%" %  float(i/dl.nums) + '\r')
        sys.stdout.flush()
    print('《一年永恒》下载完成')

posted @ 2022-06-10 11:37  CJK'sBLOG  阅读(88)  评论(0编辑  收藏  举报