爬虫机试题-爬取新闻网站

之前投简历时遇到了这样的一个笔试。本以为会是数据结构算法之类的没想到直接发了一个word直接提需求,感觉挺有意思就写了这篇文章,感兴趣的朋友可以看看。

拿到urllist

通过分析页面结构我们得以知道,这个页面本身没有新闻信息,是由js代码执行后才将信息插入到html中的,因此我们request拿到的代码是js执行前的代码,我们需要通过解析js代码来拿到想要的信息。

python 复制代码
response = requests.get(url)
response.encoding = 'utf-8'
html_content = response.text
# print(html_content)
soup = BeautifulSoup(html_content, 'html.parser')
tag = soup.findAll('script')
# print(tag[9].text)
# 使用正则表达式匹配JavaScript代码中的item数组内容
pattern = re.compile(r"item[\d+]=new title_array('([^']+)','([^']+)','([^']+)');")
# 提取item数组中的数据
matches = pattern.findall(tag[9].text)
# 处理前15个匹配项
for i, match in enumerate(matches[:15], 1):
    url, title, date = match
    print("URL:", url)
    print("Title:", title)
    print("Date:", date)

这段代码用于从首页提取新闻标题、链接和日期信息。它首先发送HTTP请求获取网页内容,然后使用BeautifulSoup库解析HTML文档。接着,通过正则表达式匹配JavaScript代码中的新闻数据,提取出匹配项,包括URL、标题和日期。最后,使用循环遍历这些匹配项,并打印输出每一条新闻的URL、标题和日期。

之后再进入详情页去拿到具体的内容。

获取详情页内容

在详情页中可以看出来,所有的正文信息都在p标签中,因此只需拿到p标签中的信息再进行筛选即可。

python 复制代码
def get_detailed(url,title,date):
    response = requests.get(url)
    response.encoding = 'utf-8'
    html_content = response.text
    # print(html_content)
    soup = BeautifulSoup(html_content, 'html.parser')
    # 使用CSS选择器定位元素
    element = soup.findAll("p")
    # 输出找到的元素
    # print(element[15:])
    data=''
    data=data+title+'\n'+date+'\n'
    for i in element[15:]:
        data=i.text+data
    print(data)

这个函数用于获取新闻的详细内容。它接收新闻的URL、标题和日期作为参数,并通过发送HTTP请求获取新闻页面的HTML内容。然后,使用BeautifulSoup库解析HTML文档,定位到新闻内容所在的段落元素。接着,将标题和日期添加到数据字符串中,并遍历段落元素,将每个段落的文本内容添加到数据字符串中。最后,将完整的新闻内容打印输出。

代码

python 复制代码
# Author: 冷月半明
# Date: 2024/4/4
# Description: This script does XYZ.
import re

import requests
from bs4 import BeautifulSoup

def get_detailed(url,title,date):
    response = requests.get(url)
    response.encoding = 'utf-8'
    html_content = response.text
    # print(html_content)
    soup = BeautifulSoup(html_content, 'html.parser')
    # 使用CSS选择器定位元素
    element = soup.findAll("p")
    # 输出找到的元素
    # print(element[15:])
    data=''
    data=data+title+'\n'+date+'\n'
    for i in element[15:]:
        data=i.text+data
    print(data)

url = '*************************'
response = requests.get(url)
response.encoding = 'utf-8'
html_content = response.text
# print(html_content)
soup = BeautifulSoup(html_content, 'html.parser')
tag = soup.findAll('script')
# print(tag[9].text)
# 使用正则表达式匹配JavaScript代码中的item数组内容
pattern = re.compile(r"item[\d+]=new title_array('([^']+)','([^']+)','([^']+)');")
# 提取item数组中的数据
matches = pattern.findall(tag[9].text)
# 处理前15个匹配项
for i, match in enumerate(matches[:15], 1):
    url, title, date = match
    print("URL:", url)
    print("Title:", title)
    print("Date:", date)
    # 调用get_detailed函数
    get_detailed(url, title,date)
相关推荐
泡泡以安18 小时前
安卓高版本HTTPS抓包:终极解决方案
爬虫·https·安卓逆向·安卓抓包
q5673152319 小时前
Java Selenium反爬虫技术方案
java·爬虫·selenium
巴里巴气1 天前
Python爬虫用Clash软件设置代理IP
爬虫·python·tcp/ip
우리帅杰11 天前
爬虫002-----urllib标准库
爬虫
RacheV+TNY26427811 天前
拼多多API限流机制破解:分布式IP池搭建与流量伪装方案
大数据·网络·人工智能·爬虫·python
我怎么又饿了呀11 天前
DataWhale-零基础络网爬虫技术(三、爬虫进阶技术)
爬虫·datawhale
network爬虫11 天前
Python异步爬虫编程技巧:从入门到高级实战指南
开发语言·爬虫·python
电商API_1800790524711 天前
实现自动胡批量抓取唯品会商品详情数据的途径分享(官方API、网页爬虫)
java·前端·爬虫·数据挖掘·网络爬虫
lynn-6611 天前
java爬虫 -jsoup的简用法
java·开发语言·爬虫
伍哥的传说11 天前
Node.js爬虫 CheerioJS ‌轻量级解析、操作和渲染HTML及XML文档
爬虫·node.js·html