10 按时间顺序爬取及批量爬取多页内容

按时间顺序爬取百度新闻

按时间排列顺序:rtt=4
按热点排列:rtt=1
具体的网页改变都会使得对应的网页链接进行变化,所有我们只需要修改url即可了。

import requests
import re
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}

def baidu(company):
    url = 'https://www.baidu.com/s?tn=news&rtt=4&bsst=1&cl=2&wd=' + company  # 把rtt参数换成4即是按时间排序,默认为1按焦点排序
    res = requests.get(url, headers=headers).text
    # print(res)

    p_href = '<h3 class="news-title_1YtI1"><a href="(.*?)"'
    href = re.findall(p_href, res, re.S)
    p_title = '<h3 class="news-title_1YtI1">.*?>(.*?)</a>'
    title = re.findall(p_title, res, re.S)
    p_date = '<span class="c-color-gray2 c-font-normal">(.*?)</span>'
    date = re.findall(p_date, res)
    p_source = '<span class="c-color-gray c-font-normal c-gap-right">(.*?)</span>'
    source = re.findall(p_source, res)

    for i in range(len(title)):  
        title[i] = title[i].strip()  # strip()函数用来取消字符串两端的换行或者空格
        title[i] = re.sub('<.*?>', '', title[i])  # 用re.sub()函数来替换不重要的内容
        print(str(i + 1) + '.' + title[i] + '(' + date[i] + '-' + source[i] + ')')
        print(href[i])


companys = ['华能信托', '阿里巴巴', '百度', '腾讯', '京东']
for i in companys:  # 这个i只是个代号,可以换成其他内容
    baidu(i)
    print(i + '爬取成功')

一次性爬取多页内容

爬取一家公司的多页信息

其实与修改按时间爬取类似,都是修改网页链接。

这是第二页:
https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&word=%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4&x_bfe_rqs=03E80&x_bfe_tjscore=0.100000&tngroupname=organic_news&newVideo=12&goods_entry_switch=1&pn=10
这是第五页:
https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&word=%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4&x_bfe_rqs=03E80&x_bfe_tjscore=0.100000&tngroupname=organic_news&newVideo=12&goods_entry_switch=1&pn=40

我们能发现只是&pn=xx变化,其余的不变,根据这个规律进行修改。

import requests
import re
import time
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}

# 爬取一个公司的多页
def baidu(page):
    num = (page - 1) * 10  # 参数规律是(页数-1)*10
    url = 'https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&word=%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4&x_bfe_rqs=03E80&x_bfe_tjscore=0.100000&tngroupname=organic_news&newVideo=12&goods_entry_switch=1&pn=' + str(num)
    res = requests.get(url, headers=headers).text
    # 其他相关爬虫代码

    p_href = '<h3 class="news-title_1YtI1"><a href="(.*?)"'
    href = re.findall(p_href, res, re.S)
    p_title = '<h3 class="news-title_1YtI1">.*?>(.*?)</a>'
    title = re.findall(p_title, res, re.S)
    p_date = '<span class="c-color-gray2 c-font-normal">(.*?)</span>'
    date = re.findall(p_date, res)
    p_source = '<span class="c-color-gray c-font-normal c-gap-right">(.*?)</span>'
    source = re.findall(p_source, res)

    for i in range(len(title)): 
        title[i] = title[i].strip()  # strip()函数用来取消字符串两端的换行或者空格
        title[i] = re.sub('<.*?>', '', title[i])  # 用re.sub()函数来替换不重要的内容
        print(str(i + 1) + '.' + title[i] + '(' + date[i] + '-' + source[i] + ')')
        print(href[i])


for i in range(10):  # 这里一共爬取了10页
    baidu(i+1)  # i是从0开始的序号,所以要写成i+1表示第几页
    print('第' + str(i+1) + '页爬取成功')  # i是从0开始的序号,所以写i+1
    time.sleep(10)  # 不要爬太快,爬太快会被反爬

爬取多家公司的多页信息

非常简单,在原先的基础上再写一个循环即可

companys = ['阿里巴巴', '百度', '腾讯', '京东']
for company in companys:
    for i in range(10):  # 这里一共爬取了10页
        baidu(company, i+1)  # i是从0开始的序号,所以要写成i+1表示第几页
        print(company + '第' + str(i+1) + '页爬取成功')  # i是从0开始的序号,所以写i+1
        time.sleep(3)  # 不要爬太快,爬太快会被反爬

补充知识点:访问超时设置——timeout参数的使用

有时访问一个网址,可能等待很久都没有反应,程序会一直等待变成假死。为避免我们需要设置访问超时
非常简单只需要加一个:

res = requests.get(url, headers=headers, timeout=10).text

当超过10s没有响应就会停止访问并报出异常,我们用try/except来处理即可