爬虫学习笔记

再次迫于生计。。。

参考了面向新人的 Python 爬虫学习资料

大致的学习路线为:

一: 简单的定向脚本爬虫( request --- bs4 --- re )

二: 大型框架式爬虫( Scrapy 框架为主)

三:浏览器模拟爬虫 ( Mechanize 模拟 和 Selenium 模拟)

有Python基础和一点html基础的话。。。貌似上手是0难度的

年轻人的第一个爬虫(虽然代码是直接copy的…

1'''
2抓取百度贴吧---生活大爆炸吧的基本内容
3爬虫线路: requests - bs4
4Python版本: 3.6
5OS: mac os 12.12.4
6'''
1import requests
2import time
3from bs4 import BeautifulSoup
# 首先我们写好抓取网页的函数
 1def get_html(url):
 2    try:
 3        r = requests.get(url, timeout=30)
 4        r.raise_for_status()
 5        # 这里我们知道百度贴吧的编码是utf-8,所以手动设置的。爬去其他的页面时建议使用:
 6        # r.endcodding = r.apparent_endconding
 7        r.encoding = 'utf-8'
 8        return r.text
 9    except:
10        return " ERROR "
1def get_content(url):
2    '''
3    分析贴吧的网页文件,整理信息,保存在列表变量中
4    '''
1    # 初始化一个列表来保存所有的帖子信息:
2    comments = []
3    # 首先,我们把需要爬取信息的网页下载到本地
4    html = get_html(url)
    # 我们来做一锅汤
    soup = BeautifulSoup(html, 'lxml')

    # 按照之前的分析,我们找到所有具有‘ j_thread_list clearfix’属性的li标签。返回一个列表类型。
    liTags = soup.find_all('li', attrs={'class': ' j_thread_list clearfix'})
 1    # 通过循环找到每个帖子里的我们需要的信息:
 2    for li in liTags:
 3        # 初始化一个字典来存储文章信息
 4        comment = {}
 5        # 这里使用一个try except 防止爬虫找不到信息从而停止运行
 6        try:
 7            # 开始筛选信息,并保存到字典中
 8            comment['title'] = li.find(
 9                'a', attrs={'class': 'j_th_tit '}).text.strip()
10            comment['link'] = "http://tieba.baidu.com/" + \
11                li.find('a', attrs={'class': 'j_th_tit '})['href']
12            comment['name'] = li.find(
13                'span', attrs={'class': 'tb_icon_author '}).text.strip()
14            comment['time'] = li.find(
15                'span', attrs={'class': 'pull-right is_show_create_time'}).text.strip()
16            comment['replyNum'] = li.find(
17                'span', attrs={'class': 'threadlist_rep_num center_text'}).text.strip()
18            comments.append(comment)
19        except:
20            print('出了点小问题')
    return comments
1def Out2File(dict):
2    '''
3    将爬取到的文件写入到本地
4    保存到当前目录的 TTBT.txt文件中。
1    '''
2    with open('TBBT.txt', 'a+') as f:
3        for comment in dict:
4            f.write('标题: {} \t 链接:{} \t 发帖人:{} \t 发帖时间:{} \t 回复数量: {} \n'.format(
5                comment['title'], comment['link'], comment['name'], comment['time'], comment['replyNum']))
        print('当前页面爬取完成')
1def main(base_url, deep):
2    url_list = []
3    # 将所有需要爬去的url存入列表
4    for i in range(0, deep):
5        url_list.append(base_url + '&pn=' + str(50 * i))
6    print('所有的网页已经下载到本地! 开始筛选信息。。。。')
1    #循环写入所有的数据
2    for url in url_list:
3        content = get_content(url)
4        Out2File(content)
5    print('所有的信息都已经保存完毕!')
1base_url = 'http://tieba.baidu.com/f?kw=&ie=utf-8'
2# 设置需要爬取的页码数量
3deep = 100
if __name__ == '__main__':
    main(base_url, deep)

年轻人的第二个爬虫:https://github.com/111qqz/spider-demo,爬了我家一周的天气情况

爬虫能够work我觉得主要取决于两个因素

一个是,一个网站的网页源码,其实是在我们本地存储的

另一个是,网页的代码是有规律的…

所以初级的爬虫的难度就仅仅在于找规律。。。然后配合chrome 开发者工具的模拟点击功能和 xpath这种文本解析工具… 就可以搞定了。。。

关于反爬虫的处理办法,以及如何提高爬虫的速度,可能才是“爬虫工程师”的核心技能?

参考资料:

BeautifulSoup官方文档,一个将html数据结构化的python库

Scrapy官方文档,一个爬虫框架