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

之前投简历时遇到了这样的一个笔试。本以为会是数据结构算法之类的没想到直接发了一个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)
相关推荐
子竹聆风7 小时前
Feapder框架UpdateItem使用技巧:如何优雅地实现"只更新有值字段"
爬虫
吴秋霖3 天前
主流反爬虫、反作弊防护与风控对抗手段
爬虫·算法·反爬虫技术
hui函数4 天前
scrapy框架-day02
后端·爬虫·python·scrapy
用户051610461674 天前
爬虫 API 技术全解析:从原理到实战的高效数据采集指南
爬虫·api
xiaoxiongip6666 天前
动态ip适合挂什么项目
网络·爬虫·python·网络协议·tcp/ip·ip
q567315236 天前
自动化拨号爬虫体系:虚拟机集群部署与增量管理
运维·爬虫·网络协议·自动化
电商API_180079052476 天前
淘宝商品视频批量自动化获取的常见渠道分享
java·爬虫·自动化·网络爬虫·音视频
果壳~7 天前
【Python】爬虫html提取内容基础,bs4
爬虫·python·html
jay神7 天前
基于Python的商品爬取与可视化系统
爬虫·python·数据分析·毕业设计·可视化系统
华科云商xiao徐8 天前
如何在C语言环境中借助Linux库构建高效网络爬虫
爬虫·数据挖掘·数据分析