python怎么批量爬取需要点击才出现的内容资源?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python怎么批量爬取需要点击才出现的内容资源?相关的知识,希望对你有一定的参考价值。

工具更好,支持登录

参考技术A

用框架啊

追问

具体怎么用?

参考技术B 可以使用selenium库去模拟点击操作
然后进行爬取

教你用Python批量爬取小说!这年头了谁看小说还充钱啊!

爬取小说的思路

  1. 首先获取小说的地址。
  2. 分析目录地址结构。
  3. 进行地址的拼接。
  4. 分析章节内容结构。
  5. 获取并保存文本。
  6. 完整代码

1.获取小说地址

加载需要的包:

import re
from bs4 import BeautifulSoup as ds
import requests

获取小说目录文件,返回<Response [200]>,表示可正常爬取该网页

base_url='https://www.soshuw.com/XuLiangShangYouWangFei/'
chapter_html=requests.get(base_url)
print(chapter_html)

2.分析小说地址结构

解析目录网页 , 输出结果为目录网页的源代码

chapter_page_html=ds(chapter_page,'lxml')
print(chapter_page)

打开目录网页,发现在正文的目录前面有一个最新章节目录(这里有九个章节),再完整的目录中是包含最新章节的,所以这里最新章节是不需要的。在这里插入图片描述
在网页单击右键选择“检查”(或者“属性”,不同的浏览器的叫法不一致,我用的是IE)选择“元素”列,鼠标再右侧代码块上移动时。左侧网页会高亮显示其对应网页区域,找到完整目录对应的代码块。如下图:在这里插入图片描述
完整目录的锚有两个,分别是class="novel_list"id=“novel108799”,仔细观察后发现class不唯一,所以我们选用id提取该块内容
在这里插入图片描述
将完整目录块提取出来

chapter_novel=chapter_page.find(id="novel108799")
print(chapter_novel)

结果如下(仅部分结果):在这里插入图片描述
对比小说章节内容网址和目录网址(base_url)发现,我们只需要将base_url和章节内容网址的后半段拼接到一起就可以得到完整的章节内容网址

3.拼接地址

利用正则语言库将地址后半段提取出来

chapter_novel_str=str(chapter_novel)
regx = '<dd><a href="/XuLiangShangYouWangFei(.*?)"'
chapter_href_list = re.findall(regx, chapter_novel_str)
print(chapter_href_list)

拼接url:
定义一个列表chapter_url_list接收完整地址

chapter_url_list = []
for i in chapter_href_list:
    url=base_url+i
    chapter_url_list.append(url)
print(chapter_url_list)

4.分析章节内容结构

打开章节,右键→“属性”,查看内容结构,发现小说正文有class和id两个锚,class是不变的,id随着章节而变化,所以我们用class提取正文
在这里插入图片描述
提取正文段

chapter_novel=chapter_page.find(id="novel108799")
print(chapter_novel)

提取正文文本和标题

body_html=requests.get('https://www.soshuw.com/XuLiangShangYouWangFei/3647144.html')
body_page=ds(body_html.content,'lxml')
body = body_page.find(class_='content')
body_content=str(body)
print(body_content)
body_regx='<br/>    (.*?)\\n'
content_list=re.findall(body_regx,body_content)
print(content_list)
title_regx = '<h1>(.*?)</h1>'
title = re.findall(title_regx, body_html.text)
print(title)

5.保存文本

with open('1.txt', 'a+') as f:
    f.write('\\n\\n')
    f.write(title[0] + '\\n')
    f.write('\\n\\n')
    for e in content_list:
        f.write(e + '\\n')
print('{} 爬取完毕'.format(title[0]))

6.完整代码

import re
from bs4 import BeautifulSoup as ds
import requests
base_url='https://www.soshuw.com/XuLiangShangYouWangFei'
chapter_html=requests.get(base_url)
chapter_page=ds(chapter_html.content,'lxml')
chapter_novel=chapter_page.find(id="novel108799")
#print(chapter_novel)
chapter_novel_str=str(chapter_novel)
regx = '<dd><a href="/XuLiangShangYouWangFei(.*?)"'
chapter_href_list = re.findall(regx, chapter_novel_str)
#print(chapter_href_list)
chapter_url_list = []
for i in chapter_href_list:
    url=base_url+i
    chapter_url_list.append(url)
#print(chapter_url_list)

for u in chapter_url_list:
    body_html=requests.get(u)
    body_page=ds(body_html.content,'lxml')
    body = body_page.find(class_='content')
    body_content=str(body)
   # print(body_content)
    body_regx='<br/>    (.*?)\\n'
    content_list=re.findall(body_regx,body_content)
    #print(content_list)
    title_regx = '<h1>(.*?)</h1>'
    title = re.findall(title_regx, body_html.text)
    #print(title)
    with open('1.txt', 'a+') as f:
        f.write('\\n\\n')
        f.write(title[0] + '\\n')
        f.write('\\n\\n')
        for e in content_list:
            f.write(e + '\\n')
    print('{} 爬取完毕'.format(title[0]))

最后说明下此文仅作学习交流,不可商用

如有疑问,欢迎在评论区一起讨论!

如有不正确的地方,欢迎指导!

以上是关于python怎么批量爬取需要点击才出现的内容资源?的主要内容,如果未能解决你的问题,请参考以下文章

python怎么获取手机通话内容?

python爬虫,一段完整的python爬虫批量下载网站图片资源的代码

python 网络爬虫,怎么自动保存图片

python之如何爬取一篇小说的第一章内容

教你用Python批量爬取小说!这年头了谁看小说还充钱啊!

Python静态网页爬取:批量获取高清壁纸