爬虫日常实战

爬取美团新闻信息,此处采用两种方法实现:

注意点:因为此处的数据都是动态数据,所以一定要考虑好向下滑动数据包会更新的情况,不然就只能读取当前页即第一页数据,方法一通过更新ajax数据包网址页数,方法二通过计算网页高度滚动到底部实现持续向下滑动过程。

方法一:

使用寻找包含数据的ajax请求(json数据)的数据包,通过jsonpath定位提取出想要的数据:

python 复制代码
# -- coding: utf-8 --
# 爬取内容:标题,标签,简介
import requests
import json
import jsonpath
import pprint

num = 1
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
    'cookie': '_lxsdk_cuid=192b4109d3bc8-0ab8530f770fd3-26001051-144000-192b4109d3bc8; logan_session_token=s9yzimqoliqqqa0xxruc; cookie_consent=true; _lxsdk_s=192b4109d3c-294-7f6-c00%7C%7C12'
}
while num <= 10:
    url = f'https://www.meituan.com/smart/view/news/r/tNewsService_pageGetByQuery?pageSize=10&pageNo={num}&newsClassifyId=&lanType=zh-CN'
    response = requests.get(url, headers=headers)
    dict_data = json.loads(response.content)
    # pprint.pprint(dict_data)
    titles = jsonpath.jsonpath(dict_data, '$..title')
    signs = jsonpath.jsonpath(dict_data, '$..newsClassifyName')
    contents = jsonpath.jsonpath(dict_data, '$..newsAbstract')
    comment_list = []
    for title, sign, comment in zip(titles, signs, contents):
        comment_dict = {
            "标题": title,
            "标签": sign,
            "简介": comment,
        }
        comment_list.append(comment_dict)
    print(json.dumps(comment_list, ensure_ascii=False, indent=4))
    num += 1

爬取结果:

方法二:

使用selenium进行自动化操作,通过xpath定位数据实现对数据的提取:

python 复制代码
# -- coding: utf-8 --
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get(
    'https://www.meituan.com/news?requestCode=b872f8728bc74f9f9c90688d88b58e1d&responseCode=ff49426a9e664f6ba92cbaa7fc9b9b08')

# 等待页面加载
time.sleep(3)
# 设置滚动和爬取参数
scroll_pause_time = 2  # 每次滚动后的等待时间
previous_height = driver.execute_script("return document.body.scrollHeight") #JavaScript 代码返回当前网页的总高度

# 循环进行滚动和数据爬取
while True:
    # 获取当前页面的元素列表
    el_list = driver.find_elements(By.XPATH, '//*[@id="__next"]/div[2]/div[2]/div/div[2]/a/div/div[1]/div')

    # 输出当前爬取的内容
    for el in el_list:
        title = el.find_element(By.XPATH, './/h2').text
        sign = el.find_element(By.XPATH, './/div[2]/span[1]/span').text
        content = el.find_element(By.XPATH, './/div[1]').text
        comment_dict = {
            "标题": title,
            "标签": sign,
            "简介": content,
        }
        print(comment_dict)  # 输出当前获取的数据

    # 滚动到页面底部
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    # 等待新内容加载
    time.sleep(scroll_pause_time)
    # 计算新的滚动高度
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == previous_height:
        break  # 如果没有更多内容,退出循环
    previous_height = new_height

driver.quit()

爬取结果:

相关推荐
ONE_Gua1 小时前
魔改chromium源码——新增自定义变量到windows属性
chrome·爬虫·浏览器
用户199701080183 小时前
深入研究:京东图片搜索商品 API 详解
大数据·爬虫·数据挖掘
昊昊该干饭了7 小时前
玩转代理 IP :实战爬虫案例
运维·服务器·爬虫·网络协议·tcp/ip·网络爬虫
攻城狮7号9 小时前
Python爬虫第9节-爬取猫眼电影排行数据的简单实战
爬虫·python·python爬虫
T - mars9 小时前
python爬虫:喜马拉雅案例(破解sign值)
javascript·爬虫·python
这里有鱼汤11 小时前
Python自动化神器Playwright:让浏览器乖乖听话的终极指南!
后端·爬虫·python
q5673152311 小时前
用Dispatch库的爬虫程序爬取图片网站
开发语言·爬虫·python·scrapy
API小爬虫12 小时前
利用 PHP 爬虫获取京东商品详情 API 返回值说明及代码示例
android·爬虫·php
大神薯条老师13 小时前
Python高级爬虫之js逆向+安卓逆向1.3节:Python数据类型
爬虫·python·深度学习·机器学习·数据分析·网络爬虫
q5673152317 小时前
使用Alamofire下载网站首页内容
开发语言·爬虫·python·scrapy·golang