zoukankan      html  css  js  c++  java
  • 爬虫实战(1)——爬取校内网招聘信息的名称

    最近焦虑感比较强,在思考自己以后从事的工作,与其凭空思考,不如来看点实际的数据,于是爬取了校内网的招聘信息研究下。        

    编写爬虫之前,我们需要先思考爬虫需要干什么、目标网站有什么特点,以及根据目标网站的数据量和数据特点选择合适的架构。编写爬虫之前,推荐使用Chrome的开发者工具来观察网页结构。在Windows和Linux,对应的快捷键是"F12"。效果如下:


    OK,可以看出,这个页面其实有一个列表,其中放着20条招聘信息。我们选中某一条信息,右键选择检查即可查看选中条目的HTML结构。如下图所示:                                                                                                                                                                                           


        

    到这一步,我们已经得到的信息有如下:

    1. 每页有20条招聘信息
    2. 招聘列表在页面上的位置为tbody的标签中。
    3. 每条招聘信息放在这个tr标签里

        完整代码如下:                                                                                                                                                                                                         

    # coding: utf-8
    
    # In[105]:
    
    import codecs
    import os 
    import requests
    from bs4 import BeautifulSoup
    
    os.chdir("D:\python study\code")
    
    DOWNLOAD_URL = 'http://www.cc98.org/list.asp?boardid=235&page=2&action='
    
    def download_page(url):
        return requests.get(url).content
    
    def parse_html(html):
        soup = BeautifulSoup(html)
        title_list_soup = soup.find('tbody')
    
        title_name_list = []
        
        for title_tr in title_list_soup.find_all('tr', attrs={'style': 'vertical-align: middle;'}):
            detail = title_tr.find('td', attrs={'style': 'text-align: justify;'})
            title_name = detail.find('span').getText()
            title_name_list.append(title_name)
    
        next_page = soup.find('div', attrs={'align': 'right'}).find('a',text="[下一页]")
        
        while i:
            return title_name_list, DOWNLOAD_URL + next_page['href']
        return title_name_list, DOWNLOAD_URL + next_page['href']
    
    def main():
        
        url = DOWNLOAD_URL
        
        with codecs.open('titles1', 'wb', encoding='utf-8') as fp:
            for i in range(4):
                html = download_page(url)
                titles1, url = parse_html(html)
                fp.write(u'{titles1}
    '.format(titles1='
    '.join(titles1)))
        
    if __name__ == '__main__':
        main()
    
    


    显示的部分结果如下:


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   


  • 相关阅读:
    Hibernate框架—简介
    ooad单例模式-Singleton
    About-JavaOOAD
    win10内置ubuntu,mysql完美清理,并安装
    VUE关于对象动态添加属性无法双向绑定问题
    正则留档
    centos下tomcat中文路径不识别
    web服务器解释html-include
    rose
    聊天框突出箭头
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9411635.html
Copyright © 2011-2022 走看看