辉煌,簇拥银烛影千行。回看处珠箔斜开,银河微亮。爬了句子控之后就再也不缺句子了哈哈,今天爬一下虫部落额热门数据,话不多说 直接开始
- 还是先分析一下网页结构,网站地址
这个网站的信息量还挺大,学术呀,合同规范,信息差等数据都有,首页居然推送了一个最新回复,这个咱们不管,进到最新热门还是先抓一下翻页的包
可见下面是有页数的
可见数据都是在html中的那就是静态数据了,先看一下详情页
连接也是一个类似id之类的东西,数据依然是在html中,再回到列表页找一下这个文章连接
连接正是在在文章标题的a标签href属性中,那就需要拼接一下连接,应该是加上域名就可以了,到这里就明了了
2. 先请求列表页并解析出来全部的文章标题和链接
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Referer': 'https://www.chongbuluo.com/forum.php?mod=guide&view=hot',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
params = {
'mod': 'guide',
'view': 'hot',
'page': '2',
}
response = requests.get('https://www.chongbuluo.com/forum.php', params=params, headers=headers)
res = etree.HTML(response.text)
data_list = res.xpath("//div[@class='bm_c']/table//tr/th/a")
for data in data_list:
title = data.xpath("./text()")[0]
url = 'https://www.chongbuluo.com/'+data.xpath("./@href")[0]
print(title,url)
然后请求详情页,还是使用GeneralNewsExtractor方法自动解析文章内容
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Referer': 'https://www.chongbuluo.com/forum.php?mod=guide&view=hot',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
response = requests.get(url, headers=headers)
extractor = GeneralNewsExtractor()
result = extractor.extract(response.text, noise_node_list=['//div[@class="comment-list"]'])
print(result)
input()
发现这个解析方法并不是完全的一无是处,解析出来当摘要看还是可以的