python 爬虫如何爬取动态生成的网页内容

--- 好的方法很多,我们先掌握一种 ---

【背景】

对于网页信息的采集,静态页面我们通常都可以通过python的request.get()库就能获取到整个页面的信息。

但是对于动态生成的网页信息来说,我们通过request.get()是获取不到。

【方法】

可以通过python第三方库selenium来配合实现信息获取,采取方案:python + request + selenium + BeautifulSoup

我们拿纵横中文网的小说采集举例(注意:请查看网站的robots协议找到可以爬取的内容,所谓盗亦有道):

思路整理:

1.通过selenium 定位元素的方式找到小说章节信息

2.通过BeautifulSoup加工后提取章节标题和对应的各章节的链接信息

3.通过request +BeautifulSoup 按章节链接提取小说内容,并将内容存储下来

【上代码】

1.先在开发者工具中,调试定位所需元素对应的xpath命令编写方式

2.通过selenium 中find_elements()定位元素的方式找到所有小说章节,我们这里定义一个方法接受参数来使用

复制代码
def Get_novel_chapters_info(url:str,xpath:str,skip_num=None,chapters_num=None):
    # skip_num 需要跳过的采集章节(默认不跳过),chapters_num需要采集的章节数(默认全部章节)
        # 创建Chrome选项(禁用图形界面)
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        driver = webdriver.Chrome(options=chrome_options)
        driver.get(url)
        driver.maximize_window()
        time.sleep(3)
        # 采集小说的章节元素
        catalogues_list = []
        try:
            catalogues = driver.find_elements(By.XPATH,xpath)
            if skip_num is None:
                for catalogue in catalogues:
                    catalogues_list.append(catalogue.get_attribute('outerHTML'))
                driver.quit()
                if chapters_num is None:
                    return catalogues_list
                else:
                    return catalogues_list[:chapters_num]
            else:
                for catalogue in catalogues[skip_num:]:
                    catalogues_list.append(catalogue.get_attribute('outerHTML'))
                driver.quit()
                if chapters_num is None:
                    return catalogues_list
                else:
                    return catalogues_list[:chapters_num]
        except Exception:
            driver.quit()

3.把采集到的信息通过beautifulsoup加工后,提取章节标题和链接内容

复制代码
        # 获取章节标题和对应的链接信息
        title_link = {}
        for each in catalogues_list:
            bs = BeautifulSoup(each,'html.parser')
            chapter = bs.find('a')
            title = chapter.text
            link = 'https:' + chapter.get('href')
            title_link[title] = link

4.通过request+BeautifulSoup 按章节链接提取小说内容,并保存到一个文件中

复制代码
        # 按章节保存小说内容
        novel_path = '小说存放的路径/小说名称.txt'
        with open(novel_path,'a') as f:
            for title,url in title_link.items():
                response = requests.get(url,headers={'user-agent':'Mozilla/5.0'})
                html = response.content.decode('utf-8')
                soup = BeautifulSoup(html,'html.parser')
                content = soup.find('div',class_='content').text
                # 先写章节标题,再写小说内容
                f.write('---小西瓜免费小说---' + '\n'*2)
                f.write(title + '\n')
                f.write(content+'\n'*3)
复制代码
相关推荐
小白学大数据9 小时前
基于Python的新闻爬虫:实时追踪行业动态
开发语言·爬虫·python
小白iP代理11 小时前
动态IP+AI反侦测:新一代爬虫如何绕过生物行为验证?
人工智能·爬虫·tcp/ip
叫我:松哥1 天前
基于网络爬虫的在线医疗咨询数据爬取与医疗服务分析系统,技术采用django+朴素贝叶斯算法+boostrap+echart可视化
人工智能·爬虫·python·算法·django·数据可视化·朴素贝叶斯
bksheng1 天前
【SSL证书校验问题】通过 monkey-patch 关掉 SSL 证书校验
网络·爬虫·python·网络协议·ssl
叫我:松哥1 天前
优秀案例:基于python django的智能家居销售数据采集和分析系统设计与实现,使用混合推荐算法和LSTM算法情感分析
爬虫·python·算法·django·lstm·智能家居·推荐算法
xnglan2 天前
使用爬虫获取游戏的iframe地址
开发语言·爬虫·python·学习
荼蘼2 天前
python爬虫实战-小案例:爬取苏宁易购的好评
开发语言·爬虫·python
香蕉可乐荷包蛋2 天前
爬虫基础概念
爬虫
小白学大数据3 天前
多线程Python爬虫:加速大规模学术文献采集
开发语言·爬虫·python·自动化
不老刘3 天前
Charles 的 Windows proxy 对爬取瑞数6 网站接口数据的作用分析
爬虫·python·drissionpage·瑞数