• python网页爬虫开发之一


    1、beautifulsoap4 和 scrapy解析和下载网页的代码区别
    bs可以离线解释html文件,但是获取html文件是由用户的其他行为的定义的,比如urllib或者request ;
    而scrapy是一个完整的获取程序,只需要把网址贴上去,就会自动去爬。 省去很多用户需要关注的细节。
    轮子和车子的区别。 前者要依附于一个程序,后者自己就能跑。
    beautifulsoap4 的性能比lxml要差
     
    2、mongodb非关系型数据库对网页的存储
    mongodb安装注意:不用选中compass,这是界面安装,需要下载,很慢
    mangodb compass单独下载安装
    ----------mangodb管理命令----------
    net start MongoDB
    net stop MongoDB
    net restart MongoDB
    安装服务
    mongod --logpath "F:mangodbDATAlogmongodb.log" --logappend --dbpath "F:mangodbDATAdatabase" --directoryperdb --install
    卸载服务(先要停止服务)
    mongod --logpath "F:mangodbDATAlogmongodb.log" --logappend --dbpath "F:mangodbDATAdatabase" --directoryperdb --remove
    重装服务
    mongod --logpath "F:mangodbDATAlogmongodb.log" --logappend --dbpath "F:mangodbDATAdatabase" --directoryperdb --reinstall
     
    3、图形界面应用开发pyqt5
     
    4、爬虫开发记录
    爬纯文本写入TXT,反爬,最多几十章。
    直接下载html文件
    5秒等待,反爬,单线程下载页面很慢——6分钟60章节
    req = request.Request(url, headers=headers)
    resp = request.urlopen(req)
    strhtml = resp.read().decode('gbk', 'ignore')
    html_soup = BeautifulSoup(strhtml, 'lxml')
    # index = BeautifulSoup(str(html_soup.find_all('div', class_='dir')), 'lxml')
    # print(html_soup.find_all(['td', ['span']]))
    body_flag = 0
    spanId = ''

    for element in html_soup.find_all(['td', ['span']]):
    if element.has_attr('id'):
    signId = element['id']
    if signId == 'jianjie': body_flag = 1
    if signId == 'xs555' or signId == 'd999': body_flag = 0
    # else:body_flag = 0

    if body_flag == 1 and element.name == 'td':
    if not element.a is None:
    chapter_name = element.string
    chapter_url = "https://www.555zw.com/book/40/40943/" + element.a.get('href')
    data = {
    'chapter_name': chapter_name,
    'chapter_url': chapter_url
    }
    chapters.insert_one(data)
     
    with open(filename, "a") as f:
    responses = request.urlopen(item["chapter_url"])
    time.sleep(5)
    contents = responses.read().decode('gbk', 'ignore').encode('utf8')
    f.write(contents)
    #origin_soup = BeautifulSoup(contents, 'lxml')
    #content = origin_soup.find(id='content')
    #move = dict.fromkeys((ord(c) for c in u"xa0 "))
    #txt = content.text.translate(move)
    #txt = content.text
  • 相关阅读:
    C# 使用NPOI 实现Excel的简单导入导出
    C# 根据Excel模版导出文件
    移动通信第九组网络抓包作业
    武金龙老师的教导(第9小组)
    【Python核心编程笔记】一、Python中一切皆对象
    自己喜欢的几个前端框架
    2018-1-6-个人博客、博客园、微信公众号、Github、本地同步写博客
    2018-1-6-IDEA快速代码生成
    CodeMirror动态修改代码(关键: editor.getDoc().setValue(data); editor.refresh();)
    FaceId枚举表(用于VBA的加载项)
  • 原文地址:https://www.cnblogs.com/joxin/p/9700753.html
Copyright © 2020-2023  润新知